gpt4 book ai didi

java - 将数组中的字符串更改为 double

转载 作者:行者123 更新时间:2023-12-01 13:42:15 25 4
gpt4 key购买 nike

我正在尝试创建一个 Arraylist,其 [0] 元素和 [1] 元素为字符串,其他元素为双倍。我想在其他类中使用这些数组作为坐标。

我收到 NullPointerException,我做错了什么?

在此处输入代码

public class ExampleTextInArrayReader
{
//naam document van ECG-patiént 1
public static final String FILE_NAME = "ecg1.txt";
//array waarin de String van ECG-waarden is omgezet in een ArrayMetDoubles
private Double[] arrayMetDoubles = new Double[722-2];
private String[] lines, gegevensPatiënt;
//velden voor patientnummer en de bijbehorende afdeling
public void main()
{
// Open the file
TextInArrayReader in = new TextInArrayReader(FILE_NAME);

// Check for errors during opening file
if (in.getError() != null)
{
System.out.println("Error opening file: " + in.getError());
System.exit(1);
}

// Read the file
String[] lines = in.getLines();
if (in.getError() != null) // check for errors during reading file
{
System.out.println("Error reading file: " + in.getError());
System.exit(1);
}

// Print file contents
for (int i = 0; i < lines.length; i++) System.out.println(lines[i]);
}

public String[] drukGegevensPatiëntAf()
{
gegevensPatiënt = new String[2];
for(int i = 0; i < 2; i++)
{
gegevensPatiënt[i] = lines[i];
}
return gegevensPatiënt;
}

public void changeArray()
{
// Get the ECG-values from the lines array and change them in doubles and put them in an arrayMetDoubles array
arrayMetDoubles = new Double[lines.length - 2];
for(int i = 2; i < arrayMetDoubles.length; i++)
{
arrayMetDoubles[i] = Double.parseDouble(lines[i + 2]);
}
}

public Double [] getLijnen()
{
changeArray();
return arrayMetDoubles;
}

}

最佳答案

因为类成员变量lines还没有赋值。在Main方法中你已经赋值给局部变量

 String[] lines = in.getLines(); 

将上面的行更改为

    lines = in.getLines(); 

在 Main() 方法中。

并确保 Main() 方法在 changeArray 方法之前调用。

关于java - 将数组中的字符串更改为 double ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20637655/

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