gpt4 book ai didi

java - 设置对象数组并在其中搜索变量

转载 作者:行者123 更新时间:2023-11-30 03:08:51 25 4
gpt4 key购买 nike

我一直在努力研究一种旨在搜索对象数组并返回特定值的方法。在我的驱动程序类中,我读取周期表 csv 文件并将每行中读取的变量拆分为Element 类的对象。在我的元素周期表类中,我创建了一个 Element 对象数组,用于保存从驱动程序读取的每个元素。我当前的“findElement”方法会导致空指针异常,并且我不确定我的对象数组是否完全按照我想要的方式执行。请随意提出建议。以下是我的类(class):(我回到了仅限驱动程序的程序)

Driver:此类打开一个输入文件并将一行拆分为 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()){
}

}

}

Element 类:保存从驱动程序读取的所有变量,并具有用于格式化它们的 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());
}
}

最佳答案

您在该代码中遇到了很多问题,其中最重要的是您的周期表看起来将包含对一个且仅一个元素的多个引用。

但至于你的问题:

  • 您的 findElement 方法不应包含 println 语句,
  • 它应该没有keyboard.next()或键盘语句。
  • 它应该简单地使用通过其参数传入的字符串,并在 elements 数组中搜索匹配的元素。
  • 您还必须修复它填充数组的方式,以便每个数组项都包含唯一的元素。

所以 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 方法。如果您不能使用它,那么您将不得不使用索引变量来跟踪已添加的元素数量,然后将最新的元素添加到下一个空数组槽中。

关于java - 设置对象数组并在其中搜索变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34096405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com