因此,对于我目前的学校项目,我们必须从一个包含元素元素周期表信息的文件中读取输入。本质上,我必须分离每一行中包含元素信息的位,并将它们放入单独的字符串值中。
这是我遇到问题的一小段代码。
for(int i=0;inputStream.hasNextLine();i++)
{
String line = inputStream.nextLine();
String[] info = line.split(",");
name=info[0];
atomicNumber=info[1];
symbol=info[2];
boilingPoint=info[3];
meltingPoint=info[4];
density=info[5];
molecularWeight=info[6];
elementInfo[i]= new Element(name,atomicNumber,symbol,boilingPoint,meltingPoint,density,molecularWeight);它把所有的东西都存储在合适的地方,除了密度和分子量的信息,我得到的是空值。我找不到任何信息,为什么它不工作的最后两个字符串。
输出示例:
元素名称: actinium
原子序数: 89
符号: Ac
沸点: 3470
熔点: 1324
密度:空
分子量:空
下面是element对象的构造函数:
public Element(String name,String atomicNumber,String symbol, String boilingPoint, String meltingPoint, String density, String molecularWeight)
{
this.name=name;
this.atomicNumber=atomicNumber;
this.symbol=symbol;
this.boilingPoint=boilingPoint;
this.meltingPoint=meltingPoint;
this.density=density;
this.molecularWeight=molecularWeight;
}发布于 2015-11-30 03:46:34
你可以试试这个,
//表示进入文件的信息不存在,则取其缺省值即为空
info[5] == null ? "empty" : info[5];
info[6] == null ? "empty" : info[6];发布于 2015-11-30 03:55:49
毫无疑问,您正在读取的文件包含7个元素,否则以下代码将导致错误
density=info5;molecularWeight=info6;
示例:
public static void main(String args[]) {
String line = "1,2,3,4,5,,";
String[] info = line.split(",");
System.out.println(info.length);
System.out.println(Arrays.deepToString(info));
}上面代码片段的输出是5和1,2,3,4,5,这里我们不能使用info5或info6,因为它会导致错误。
因此,您的数据是正确的,并且您正在捕获所有值。我认为问题出在打印输出上,但是您没有在查询中提到要深入调查的代码。
希望这能帮上忙。
发布于 2015-11-30 04:40:10
String.split()从不返回null (参见here,这意味着问题不在于split()方法,而在于其他地方。split()方法似乎至少返回7个数据块,这是因为当您执行此molecularWeight=info[6]时,您不会获得ArrayIndexOutOfBoundException。
然后问题出现在其他地方,你可以通过检查你的代码来发现,你肯定遗漏了一些东西,一些非常非常简单的东西。
让我们假设您有以下输入(2个元素):
actinium,89,Da,3470,1926,missing-x,missing-y
actinium,13,Gk,5480,1124,missing-z,missing-w我使用了您的大部分代码,并开发了一个示例用例来从文件中读取上述两个元素,并将它们存储在一个列表中,然后将它们打印回来。我使用了List<Element>而不是您的Element[]解决方案,并在Element类中覆盖了toString(),以便使用Java8的流漂亮地打印元素,如下所示,并与您的解决方案进行比较:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ReadFromFileElements {
public static void main(String... args) throws FileNotFoundException {
// You can use list instead of Element[] array
List<Element> elementInfo = new ArrayList<Element>();
//file containing the input
File file = new File("C:\\test_java\\elements.txt");
//open input stream to the file
Scanner input = new Scanner(file);
//as long as there is nextLine() keep looping
while(input.hasNextLine()) {
String line = input.nextLine();
String[] chunk = line.split(",");
Element e = new Element(chunk[0], chunk[1], chunk[2], chunk[3], chunk[4], chunk[5],
chunk[6]);
//add to the list of Element/s
elementInfo.add(e);
}
//close input stream
input.close();
//java 8 stream iterator through collection
elementInfo.stream().forEach((temp) -> {
//temp.toString() uses the overrided toString() of element class
System.out.println(temp.toString());
});
}
}
class Element {
String name;
String atomicNumber;
String symbol;
String boilingPoint;
String meltingPoint;
String density;
String molecularWeight;
public Element(String name, String atomicNumber, String symbol, String boilingPoint, String meltingPoint,
String density, String molecularWeight) {
this.name = name;
this.atomicNumber = atomicNumber;
this.symbol = symbol;
this.boilingPoint = boilingPoint;
this.meltingPoint = meltingPoint;
this.density = density;
this.molecularWeight = molecularWeight;
}
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("\n Element name: " + this.name);
builder.append("\n Atomic no: " + this.atomicNumber);
builder.append("\n Symobl : " + this.symbol);
builder.append("\n Boiling point : " + this.boilingPoint);
builder.append("\n Melting point : " + this.meltingPoint);
builder.append("\n Density : " + this.density);
builder.append("\n Molecular weight: " + this.molecularWeight);
return builder.toString();
}
}并针对文件中的上述两行运行上述代码,我得到以下输入:
Element name: actinium
Atomic no: 89
Symobl : Da
Boiling point : 3470
Melting point : 1926
Density : missing-x
Molecular weight: missing-y
Element name: actinium
Atomic no: 13
Symobl : Gk
Boiling point : 5480
Melting point : 1124
Density : missing-z
Molecular weight: missing-whttps://stackoverflow.com/questions/33987190
复制相似问题