我一直在努力寻找一种方法来搜索对象数组并返回一个特定的值。在我的驱动程序类中,我读取了周期表csv文件并将每行中读取的变量拆分发送给Element类的一个对象。在我的周期表类中,我创建了一个Element对象数组,用于保存从驱动程序读取的每个元素。我当前的“findElement”方法导致nullpointer异常,我不确定我的对象数组是否完全按照我的要求进行操作。随意抛弃建议。以下是我的课程:(我回到了仅限驾驶员的课程)
驱动程序:此类打开一个输入文件,并将一行拆分为7个不同的变量,然后发送给Element类对象。
public class PeriodicTableDriver
{
public static void main(String[] args)
{
Scanner keyboard= new Scanner(System.in);
Scanner inputStream=null;
String elementName="";
String atomicNumber="";
String symbol="";
double boilingPoint=0;
double meltingPoint=0;
double density=0;
double molecularWeight=0;
int choice=0;
String fileName1= "PeriodicTableData.csv";
String fileName2= "MolecularWeightInput.txt";
PeriodicTable periodicTable= new PeriodicTable();
try
{
inputStream=new Scanner(new File(fileName1));
}
catch(FileNotFoundException e){
System.out.println("Error opening the file.");
System.exit(0);
}
int count=0;
String title=inputStream.nextLine();
while(inputStream.hasNext()){
String periodicInfo=inputStream.nextLine();
String[] PeriodicTableData= periodicInfo.split(",");
elementName=PeriodicTableData [0];
atomicNumber= PeriodicTableData [1];
symbol= PeriodicTableData [2];
if (PeriodicTableData[3].equals(""))
boilingPoint = 0;
else
boilingPoint = Double.parseDouble(PeriodicTableData[3]);
if (PeriodicTableData[4].equals(""))
meltingPoint = 0;
else
meltingPoint = Double.parseDouble(PeriodicTableData[4]);
if (PeriodicTableData[5].equals(""))
density = 0;
else
density = Double.parseDouble(PeriodicTableData[5]);
if (PeriodicTableData[6].equals(""))
molecularWeight = 0;
else
molecularWeight = Double.parseDouble(PeriodicTableData[6]);
count++;
Element element= new Element(count,elementName,atomicNumber,symbol,boilingPoint,meltingPoint,density,molecularWeight);
periodicTable.readPeriodicTableInfo(element);
periodicTable.displayElement(symbol);
}
try
{
inputStream=new Scanner(new File(fileName2));
}
catch(FileNotFoundException e){
System.out.println("Error opening the file.");
System.exit(0);
}
while(inputStream.hasNextLine()){
}
}
}
元素类:保存从驱动程序读取的所有变量,并具有用于格式化它们的toString方法
public class Element
{
String elementName;
String atomicNumber;
String symbol;
double boilingPoint;
double meltingPoint;
double density;
double molecularWeight;
int count;
public Element(int count, String elementName, String atomicNumber, String symbol,
double boilingPoint, double meltingPoint, double density,
double molecularWeight)
{
super();
this.count=count;
this.elementName = elementName;
this.atomicNumber = atomicNumber;
this.symbol = symbol;
this.boilingPoint = boilingPoint;
this.meltingPoint = meltingPoint;
this.density = density;
this.molecularWeight = molecularWeight;
}
public String toString(){
String element = "Element name: " + elementName
+ "\nAtomic Number: " + atomicNumber
+ "\nSymbol: " + symbol;
if (boilingPoint == 0)
{
element = element + "\nBoiling Point: unknown";
}
else
{
element = element + "\nBoiling Point: " + boilingPoint + " K";
}
if (meltingPoint == 0)
{
element = element + "\nMelting Point: unknown";
}
else
{
element = element + "\nMelting Point: " + meltingPoint + " K";
}
if (density == 0)
{
element = element + "\nDensity: unknown";
}
else
{
element = element + "\nDensity: " + density + " g/L";
}
element=element+"\nMolecular Weight: " + molecularWeight + "g/mole";
return element;
}
/**
* @return the elementName
*/
public String getElementName()
{
return elementName;
}
/**
* @return the atomicNumber
*/
public String getAtomicNumber()
{
return atomicNumber;
}
/**
* @return the symbol
*/
public String getSymbol()
{
return symbol;
}
/**
* @return the boilingPoint
*/
public double getBoilingPoint()
{
return boilingPoint;
}
/**
* @return the meltingPoint
*/
public double getMeltingPoint()
{
return meltingPoint;
}
/**
* @return the density
*/
public double getDensity()
{
return density;
}
/**
* @return the molecularWeight
*/
public double getMolecularWeight()
{
return molecularWeight;
}
/**
* @return the count
*/
public int getCount()
{
return count;
}
}
PeriodicTable类:保存将满足菜单项的方法
public class PeriodicTable
{
private final static int ARRAY_SIZE= 120;
private Element[] elements;
private int count=110;
Scanner keyboard= new Scanner(System.in);
public PeriodicTable(){
elements = new Element[ARRAY_SIZE];
}
public void readPeriodicTableInfo(Element element){
for(int i=0; i<elements.length;i++){
elements[i]=element;
System.out.println(elements[i].toString());
}
}
public void displayMenu(){
System.out.println("1. Display information for all elements in the Periodic Table");
System.out.println("2. Display information for one element");
System.out.println("3. Display particle information for one element");
System.out.println("4. Display the element with the highest boiling point");
System.out.println("5. Display the element with the lowest melting point");
System.out.println("6. Display the molecular mass calculations for elements in file");
System.out.println("7. Quit");
System.out.print("Please enter your choice: ");
}
public int findElement(String symbol){
System.out.println("Enter element symbol: ");
String elementSymbol=keyboard.next();
for(int i=0; i<elements.length;i++){
if(elementSymbol.equalsIgnoreCase(elements[i].getSymbol())){
return i;
}
}
return -1;
}
public void displayElement(String symbol){
System.out.println();
System.out.println(elements[findElement(symbol)].toString());
}
}
答案 0 :(得分:2)
您在该代码中遇到了很多问题,其中您的PeriodicTable看起来很少会对一个且只有一个元素进行多次引用。
但是关于你的问题:
keyboard.next()
或键盘语句。因此,findElement应该更加简单,例如:
public int findElement(String symbol){
for(int i=0; i<elements.length;i++) {
if(symbol.equalsIgnoreCase(elements[i].getSymbol())){
return i;
}
}
// I'd throw an exception here if no element is found
}
更好的方法是让方法返回找到的Element对象而不是int数组索引。
另外,一方建议:请查阅并尝试遵循Java代码格式规则。遵循这些规则,其他人将更容易阅读和理解您的代码,然后能够帮助您。如果您使用的是大多数IDE,他们可以帮助您正确格式化代码。
编辑:这似乎是最破碎的方法:
public void readPeriodicTableInfo(Element element){
for (int i=0; i<elements.length;i++) {
elements[i]=element;
System.out.println(elements[i].toString());
}
}
让我们看看它的作用:它遍历整个元素数组,将传递给此方法的元素放入每个项目中阵列,我真的不认为你想要发生的事情。相反,您应该将传入的元素添加到数组中的一个且只有一个项目。这最好通过将数组中的元素更改为ArrayList<Element>
来执行,然后您可以简单地调用ArrayList add方法。如果你不能使用它,那么你将不得不使用索引变量来跟踪已经添加了多少元素,然后将最新的元素添加到下一个空数组槽中。 / p>