-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElementReader.java
More file actions
79 lines (66 loc) · 1.89 KB
/
Copy pathElementReader.java
File metadata and controls
79 lines (66 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.util.*;
import java.io.*;
public class ElementReader
{
private String filename;
private PeriodicTable periodicTable;
private FileInputStream inputStream;
private InputStreamReader reader;
private BufferedReader bufferedReader;
public ElementReader(String filename, PeriodicTable periodicTable) {
this.filename = filename;
this.periodicTable = periodicTable;
try
{
inputStream = new FileInputStream(filename);
reader = new InputStreamReader(inputStream);
bufferedReader = new BufferedReader(reader);
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
}
public void readToPeriodicTable() {
String line;
try
{
do {
line = bufferedReader.readLine();
if (line != null) {
System.out.println(line);
convertToElement(line);
}
} while (line != null);
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
}
private void convertToElement(String line) {
String[] elements = line.split("/>");
for(int i = 0; i < elements.length; i++)
{
String element = elements[i];
element = element.replace("<", "");
String[] tokens = element.split(",");
String symbol = tokens[0];
String name = tokens[1];
int atomicNum = Integer.parseInt(tokens[2]);
double mass = Double.parseDouble(tokens[3]);
// Non metal
if (tokens[4].charAt(0) == 'S' || tokens[4].charAt(0) == 'L' || tokens[4].charAt(0) == 'G')
{
char state = tokens[4].charAt(0);
periodicTable.add(new NonMetal(symbol, name, atomicNum, mass, state));
}
else
{
double conductivity = Double.parseDouble(tokens[4]);
periodicTable.add(new Metal(symbol, name, atomicNum, mass, conductivity));
}
// System.out.println(periodicTable.currentPointer());
}
}
}