2016-06-03 1 views
0

.dat 파일을 사용하여 주기적 요소를 찾는 프로그램이 있습니다. 그러나 번호 나 이름으로 요소를 찾으려고 할 때마다 계속 찾을 수 없습니다.문자열을 출력 할 수 있도록 Java에서 문자열에 액세스하려고 시도했지만 문자열을 찾을 수 없습니다.

또한 NumberFormatException 문제가 있습니다. 매번 옵션 3을 사용할 때 null이됩니다.

도움이 될만한 도움이 될 것입니다 !!!

import java.util.Scanner; 
import java.util.Arrays; 
import java.io.File; 
import java.io.FileNotFoundException; 

class PeriodicElement{ 
    private int atomicNum; 
    private float atomicWeight; 
    private String abbreviation , name; 

PeriodicElement(int atomicNum , String abbreviation , String name , float atomicWeight){ 
    this.atomicNum = atomicNum; 
    this.abbreviation = abbreviation; 
    this.name   = name; 
    this.atomicWeight = atomicWeight; 
} 

public int getNumber(){ 
    return atomicNum; 
} 

public String getAbb(){ 
    return abbreviation; 
} 

public String getName(){ 
    return name; 
} 

public float getWeight(){ 
    return atomicWeight; 
} 

public String toString(){ 
    return String.format("%3d %-3s %-20s %5.2f", atomicNum, abbreviation, name, atomicWeight); 
} 
} 
class PeriodicTable{ 
    private String fileName = "periodictable.dat"; 
    private String[][] table = new String[200][4]; 

PeriodicTable(String fileName){ 
    readTable(fileName); 
} 

public String[][] readTable(String fileName){ 
    Scanner inFile = null; 
    try{ 
     inFile = new Scanner(new File(fileName)); 
    }catch(FileNotFoundException nf){ 
     System.out.println(fileName + " not found"); 
     System.exit(0); 
    } 

    table = new String[200][4]; 
    int i = 0; 
    while (inFile.hasNext() && i < table.length){ 
     int number   = inFile.nextInt(); 
     String abbreviation = inFile.next(); 
     String name   = inFile.next(); 
     float weight   = inFile.nextFloat(); 
     String strNumber = String.valueOf(number); 
     String strWeight = String.valueOf(weight); 
     table[i][0] = strNumber; 
     table[i][1] = abbreviation; 
     table[i][2] = name; 
     table[i][3] = strWeight; 
     i++; 
    } 
    inFile.close(); 
    return table; 
} 

public int getNumberOfElements(){ 
    int counter = 0; 
    for(int i = 0; i < table.length; i++){ 
     if (table[i][0] != null){ 
      counter++; 
     } 
    } 
    return counter; 
} 

public String findElement(String number){ 
    for(int i = 0; i < table.length; i++){ 
     if(number == table[i][0]){ 
      String strAtomicNum  = table[i][0]; 
      String abbreviation  = table[i][1]; 
      String name    = table[i][2]; 
      String strAtomicWeight = table[i][3]; 

      int atomicNumber = Integer.parseInt(strAtomicNum); 
      float atomicWeight = Float.parseFloat(strAtomicWeight); 

      PeriodicElement element = new PeriodicElement(atomicNumber , abbreviation, 
             name , atomicWeight); 
      return element.toString(); 
     } 
    } 
    return null; 
} 

public String findAbbreviation(String abbreviation){ 
    for(int i = 0; i < table.length; i++){ 
     if(abbreviation == table[i][1]){ 
      String strAtomicNum  = table[i][0]; 
      String abbreviation1  = table[i][1]; 
      String name    = table[i][2]; 
      String strAtomicWeight = table[i][3]; 

      int atomicNumber = Integer.parseInt(strAtomicNum); 
      float atomicWeight = Float.parseFloat(strAtomicWeight); 

      PeriodicElement element = new PeriodicElement(atomicNumber , abbreviation1, 
             name , atomicWeight); 
      return element.toString(); 
     } 
    } 
    return null; 
} 

public String printTable(){ 
    System.out.println("### $$$ $$$$$$$$$$$$$$$$$$$$ ###.##\n"); 
    for(int i = 0; i < table.length; i++){ 
     String strAtomicNum  = table[i][0]; 
     String abbreviation1  = table[i][1]; 
     String name    = table[i][2]; 
     String strAtomicWeight = table[i][3]; 

     int atomicNumber = Integer.parseInt(strAtomicNum); 
     float atomicWeight = Float.parseFloat(strAtomicWeight); 

     PeriodicElement element = new PeriodicElement(atomicNumber , abbreviation1, 
            name , atomicWeight); 
     String elementInfo = element.toString(); 
     if (table[i][0] != null){ 
      System.out.println(elementInfo); 
     } 
    } 
    return null; 
} 

public static void main(String[] args){ 
    System.out.println("Periodic Table by Ronald Lencevicius.\n"); 

    PeriodicTable table = new PeriodicTable("periodictable.dat"); 
    int numberElements = table.getNumberOfElements(); 
    System.out.println("Number of elements: " + numberElements + "\n"); 

    Scanner kb = new Scanner(System.in); 
    Scanner kb1 = new Scanner(System.in); 
    Scanner kb2 = new Scanner(System.in); 
    int choice = 0; 
    while(choice != 4){ 
     System.out.println("1. Search atomic number"); 
     System.out.println("2. Search abbreviation"); 
     System.out.println("3. Print table"); 
     System.out.println("4. Exit\n"); 
     System.out.println("Enter a choice?"); 

     choice = kb.nextInt(); 

     if(choice == 1){ 
      System.out.println("Enter atomic number: "); 
      String elementNum = kb1.next(); 

      String element = table.findElement(elementNum); 
      if(element != null){ 
       System.out.println("### $$$ $$$$$$$$$$$$$$$$$$$$ ###.##\n" + 
          element); 
      }else{ 
       System.out.println("Not found"); 
      } 
     }else if(choice == 2){ 
      System.out.println("Enter abbreviation: "); 
      String abbrev = kb2.next(); 

      String element = table.findAbbreviation(abbrev); 
      if(element != null){ 
       System.out.println("### $$$ $$$$$$$$$$$$$$$$$$$$ ###.##\n" + 
          element); 
      }else{ 
       System.out.println("Not found"); 
      } 
     }else if(choice == 3){ 
      table.printTable(); 
     }else if(choice == 4){ 
      System.exit(0); 
     } 
    } 
} 
} 

다음은 .dat 파일입니다. 자바 (프리미티브 제외) 참조와 함께 작동

1 H Hydrogen 1.00794 
2 He  Helium 4.002602 
3 Li  Lithium  6.941 
4 Be  Beryllium 9.012182 
5 B Boron 10.811 
6 C Carbon 12.0107 
7 N Nitrogen 14.0067 
8 O Oxygen 15.9994 
9 F Fluorine 18.9984032 
10 Ne  Neon 20.1797 
11 Na  Sodium 22.98976928 
12 Mg  Magnesium 24.3050 
13 Al  Aluminum 26.9815386 
14 Si  Silicon  28.0855 
15 P Phosphorus 30.973762 
16 S Sulfur 32.065 
17 Cl  Chlorine 35.453 
18 Ar  Argon 39.948 
19 K Potassium 39.0983 
20 Ca  Calcium  40.078 
21 Sc  Scandium 44.955912 
22 Ti  Titanium 47.867 
23 V Vanadium 50.9415 
24 Cr  Chromium 51.9961 
25 Mn  Manganese 54.938045 
26 Fe  Iron 55.845 
27 Co  Cobalt 58.933195 
28 Ni  Nickel 58.6934 
29 Cu  Copper 63.546 
30 Zn  Zinc 65.38 
31 Ga  Gallium  69.723 
32 Ge  Germanium 72.64 
33 As  Arsenic  74.92160 
34 Se  Selenium 78.96 
35 Br  Bromine  79.904 
36 Kr  Krypton  83.798 
37 Rb  Rubidium 85.4678 
38 Sr  Strontium 87.62 
39 Y Yttrium  88.90585 
40 Zr  Zirconium 91.224 
41 Nb  Niobium  92.90638 
42 Mo  Molybdenum 95.96 
43 Tc  Technetium 0 
44 Ru  Ruthenium 101.07 
45 Rh  Rhodium  102.90550 
46 Pd  Palladium 106.42 
47 Ag  Silver 107.8682 
48 Cd  Cadmium  112.411 
49 In  Indium 114.818 
50 Sn  Tin  118.710 
51 Sb  Antimony 121.760 
52 Te  Tellurium 127.60 
53 I Iodine 126.90447 
54 Xe  Xenon 131.293 
55 Cs  Cesium 132.9054519 
56 Ba  Barium 137.327 
57 La  Lanthanum 138.90547 
58 Ce  Cerium 140.116 
59 Pr  Praseodymium 140.90765 
60 Nd  Neodymium 144.242 
61 Pm  Promethium 0 
62 Sm  Samarium 150.36 
63 Eu  Europium 151.964 
64 Gd  Gadolinium 157.25 
65 Tb  Terbium  158.92535 
66 Dy  Dysprosium 162.500 
67 Ho  Holmium  164.93032 
68 Er  Erbium 167.259 
69 Tm  Thulium  168.93421 
70 Yb  Ytterbium 173.054 
71 Lu  Lutetium 174.9668 
72 Hf  Hafnium  178.49 
73 Ta  Tantalum 180.94788 
74 W Tungsten 183.84 
75 Re  Rhenium  186.207 
76 Os  Osmium 190.23 
77 Ir  Iridium  192.217 
78 Pt  Platinum 195.084 
79 Au  Gold 196.966569 
80 Hg  Mercury  200.59 
81 Tl  Thallium 204.3833 
82 Pb  Lead 207.2 
83 Bi  Bismuth  208.98040 
84 Po  Polonium 0 
85 At  Astatine 0 
86 Rn  Radon 0 
87 Fr  Francium 0 
88 Ra  Radium 0 
+0

와 동일. – StephaneM

+0

readTable() 메소드에서 반환하는 테이블을 공유 할 수 있습니까? – erolkaya84

+0

방금 ​​게시물을 추가했습니다 – 392781

답변

2

그래서 당신은 당신이 자신의 을 비교하려면 두 변수의 메모리 주소를 비교하는 findElement에

if(number == table[i][0]) 

을 수행 할 때. 그냥 그들이 INT/float로 변환 할 수없는 경우 자신의 가치를 볼 수 strAtomicNum`과`strAtomicWeight``의 값에 println하려고

if(number.equals(table[i][0])) 

에 그 변경하고 findAbbreviation

+0

정말 고마워요 !!!!!! 나는 밤새도록 여기에 있었어 ... 너 정말로 나를 구해 줬어. – 392781

+0

문제 없습니다. 그러나 Jägermeister가 말한 것처럼 파일의 각 줄에 String [] 대신 Class를 사용하면 시간이 많이 걸릴 것입니다. 그런 식으로 어떤 이유에서든 파일이 변경되면 그 메서드를 변경해야만 모든 메서드 대신 클래스 인스턴스가됩니다. – dtortola

+0

네가 살고 배우는 것 같아. 나는 정직하게 그다지 좋지 않다. 그리고 학급은 나에게 많은 문제를 준다. 그래도 연습 할게. – 392781

관련 문제