我一直试图从我使用BufferedReader制作的文本文件中导入几种化学元素的属性。我打算将每个属性保存为变量,然后为每个元素创建一个对象数组及其相关属性。
我的代码如下所示:
BufferedReader reader = null;
ElementsTest[] element = new ElementsTest[118];
try {
File file = new File("Elements.txt");
reader = new BufferedReader(new FileReader(file));
for (int counter = 0 ; counter < 118 ; counter++)
{
String name = reader.readLine();
int atomicNum = counter + 1;
String atomicWeight = reader.readLine();
String elementSymbol = reader.readLine();
String elementCharge = reader.readLine();
String fullElectronConfig = reader.readLine();
String shortElectronConfig = reader.readLine();
String elementState = reader.readLine();
String elementType = reader.readLine();
String density = reader.readLine();
String meltingPoint = reader.readLine();
String boilingPoint = reader.readLine();
String emptyLine = reader.readLine();
element[counter] = new ElementsTest (name, atomicNum, atomicWeight, elementSymbol, elementCharge, fullElectronConfig, shortElectronConfig, elementState, elementType, density, meltingPoint, boilingPoint);
}
以下是文本文件的样子(118个元素中的一小段摘录):
Hydrogen
1.008
H
+1
1s^1
N/A
Gas
Non-Metal
0.08988
-259.16
-252.879
Helium
4.003
He
0
1s2
N/A
Gas
Noble Gas
0.1786
−272.20
−268.928
Lithium
6.941
He
+1
N/A
[He] 2s1
Solid
Alkali Metal
0.534
180.50
1330
Beryllium
9.012
Be
+2
N/A
[He] 2s2
Solid
Alkaline Earth Metal
1.85
1287
2970
程序编译并运行,但是输出不正确(我将按钮编码为将文本设置为对象的elementSymbol,而是获得了大量属性)。
以下是我的GUI代码的其余部分,如果这有用的话:
JButton[] buttonArray = new JButton[118];
for (int i = 0 ; i < 118 ; i++)
{
buttonArray[i] = new JButton();
buttonArray[i].setPreferredSize(new Dimension(10, 10));
}
for (int counter2 = 0 ; counter2 < 118 ; counter2++)
{
String currentSymbol = element[counter2].getElementSymbol();
buttonArray[counter2].setText(currentSymbol);
if (element[counter2].getElementType().equals("Metal"))
{
buttonArray[counter2].setBackground(Color.ORANGE);
buttonArray[counter2].setOpaque(true);
buttonArray[counter2].setBorderPainted(false);
}
if (element[counter2].getElementType().equals("Non-Metal"))
{
buttonArray[counter2].setBackground(Color.GRAY);
buttonArray[counter2].setOpaque(true);
buttonArray[counter2].setBorderPainted(false);
}
if (element[counter2].getElementType().equals("Noble Gas"))
{
buttonArray[counter2].setBackground(Color.PINK);
buttonArray[counter2].setOpaque(true);
buttonArray[counter2].setBorderPainted(false);
}
if (element[counter2].getElementType().equals("Transition Metal"))
{
buttonArray[counter2].setBackground(Color.GREEN);
buttonArray[counter2].setOpaque(true);
buttonArray[counter2].setBorderPainted(false);
}
if (element[counter2].getElementType().equals("Lanthanide"))
{
buttonArray[counter2].setBackground(Color.YELLOW);
buttonArray[counter2].setOpaque(true);
buttonArray[counter2].setBorderPainted(false);
}
if (element[counter2].getElementType().equals("Alkali Metal"))
{
buttonArray[counter2].setBackground(Color.RED);
buttonArray[counter2].setOpaque(true);
buttonArray[counter2].setBorderPainted(false);
}
if (element[counter2].getElementType().equals("Alkaline Earth Metal"))
{
buttonArray[counter2].setBackground(Color.BLUE);
buttonArray[counter2].setOpaque(true);
buttonArray[counter2].setBorderPainted(false);
}
if (element[counter2].getElementType().equals("Halogen"))
{
buttonArray[counter2].setBackground(Color.MAGENTA);
buttonArray[counter2].setOpaque(true);
buttonArray[counter2].setBorderPainted(false);
}
if (element[counter2].getElementType().equals("Metalloid"))
{
buttonArray[counter2].setBackground(Color.CYAN);
buttonArray[counter2].setOpaque(true);
buttonArray[counter2].setBorderPainted(false);
}
if (element[counter2].getElementType().equals("Actinide"))
{
buttonArray[counter2].setBackground(Color.LIGHT_GRAY);
buttonArray[counter2].setOpaque(true);
buttonArray[counter2].setBorderPainted(false);
}
else {
buttonArray[counter2].setOpaque(true);
}
}
metalsPanel.add(buttonArray[0]);
metalsPanel.add(new JButton(""));
metalsPanel.add(buttonArray[2]);
metalsPanel.add(buttonArray[3]);
metalsPanel.add(buttonArray[10]);
metalsPanel.add(buttonArray[11]);
metalsPanel.add(buttonArray[18]);
metalsPanel.add(buttonArray[19]);
metalsPanel.add(buttonArray[36]);
metalsPanel.add(buttonArray[37]);
metalsPanel.add(buttonArray[54]);
metalsPanel.add(buttonArray[55]);
metalsPanel.add(buttonArray[86]);
metalsPanel.add(buttonArray[87]);
for (int x = 20 ; x < 30 ; x++)
{
transitionMetalsPanel.add(buttonArray[x]);
}
for (int x = 38 ; x < 48 ; x++)
{
transitionMetalsPanel.add(buttonArray[x]);
}
transitionMetalsPanel.add(new JButton("57-71"));
for (int x = 71 ; x < 80 ; x++)
{
transitionMetalsPanel.add(buttonArray[x]);
}
transitionMetalsPanel.add(new JButton("89-103"));
for (int x = 103 ; x < 112 ; x++)
{
transitionMetalsPanel.add(buttonArray[x]);
}
nonMetalsPanel.add(new JButton(""));
nonMetalsPanel.add(new JButton(""));
nonMetalsPanel.add(new JButton(""));
nonMetalsPanel.add(new JButton(""));
nonMetalsPanel.add(new JButton(""));
nonMetalsPanel.add(buttonArray[1]);
for (int x = 4 ; x < 10 ; x++)
{
nonMetalsPanel.add(buttonArray[x]);
}
for (int x = 12 ; x < 18 ; x++)
{
nonMetalsPanel.add(buttonArray[x]);
}
for (int x = 30 ; x < 36 ; x++)
{
nonMetalsPanel.add(buttonArray[x]);
}
for (int x = 48 ; x < 54 ; x++)
{
nonMetalsPanel.add(buttonArray[x]);
}
for (int x = 80 ; x < 86 ; x++)
{
nonMetalsPanel.add(buttonArray[x]);
}
for (int x = 112 ; x < 118 ; x++)
{
nonMetalsPanel.add(buttonArray[x]);
}
for (int x = 56 ; x < 71 ; x++)
{
lanthanidesPanel.add(buttonArray[x]);
}
for (int x = 88 ; x < 103 ; x++)
{
lanthanidesPanel.add(buttonArray[x]);
}
window.setVisible (true);
window.pack();
}
这是ElementsTest类,它包含其属性的检索方法:
public class ElementsTest extends Object
{
private String name;
private int atomicNum;
private String atomicWeight;
private String elementSymbol;
private String elementCharge;
private String fullElectronConfig;
private String shortElectronConfig;
private String elementState;
private String elementType;
private String density;
private String meltingPoint;
private String boilingPoint;
public ElementsTest (String name, int atomicNum, String atomicWeight, String elementSymbol, String elementCharge, String fullElectronConfig, String shortElectronConfig, String elementState, String elementType, String density, String meltingPoint, String boilingPoint)
{
super();
this.name = name;
this.atomicNum = atomicNum;
this.atomicWeight = atomicWeight;
this.elementSymbol = elementSymbol;
this.elementCharge = elementCharge;
this.fullElectronConfig = fullElectronConfig;
this.shortElectronConfig = shortElectronConfig;
this.elementState = elementState;
this.elementType = elementType;
this.density = density;
this.meltingPoint = meltingPoint;
this.boilingPoint = boilingPoint;
}
public String getName()
{
return this.name;
}
public String getAtomicWeight()
{
return this.atomicWeight;
}
public String getElementSymbol()
{
return this.elementSymbol;
}
public String getElementCharge()
{
return this.elementCharge;
}
public String getFullElectronConfig()
{
return this.fullElectronConfig;
}
public String getShortElectronConfig()
{
return this.shortElectronConfig;
}
public String getElementState()
{
return this.elementState;
}
public String getElementType()
{
return this.elementType;
}
public String getDensity()
{
return this.density;
}
public String getMeltingPoint()
{
return this.meltingPoint;
}
public String getBoilingPoint()
{
return this.boilingPoint;
}
}
我已经多次浏览了我的文本文件,但我似乎无法找到输入可能出错的地方QQ任何帮助都表示赞赏。