[英]changing string to double in array
I am trying to make an Arraylist with its [0] element and [1] element a string and the others doubles. 我正在尝试使用其[0]元素和[1]元素使一个Arraylist成为字符串,而其他元素加倍。 I want to use these arrays for my coordinates in my other classes.
我想在其他类中将这些数组用作坐标。
I get an NullPointerException, what am i doing wrong? 我收到NullPointerException,我在做什么错?
enter code here 在这里输入代码
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;
}
} }
Becuase the class member varaible lines is not yet assigned.In the Main method you have assingned to the local variable 因为尚未分配类成员可变行。在Main方法中,您已经赋给了局部变量
String[] lines = in.getLines();
Change the above line to 将上面的行更改为
lines = in.getLines();
in Main() method. 在Main()方法中。
And Make sure the Main() method called before changeArray Method . 并确保在changeArray方法之前调用Main()方法。
I like how you mixed Nederlands with English throughout your code! 我喜欢您在整个代码中如何将Nederlands与英语混合在一起! :)
:)
Anyway, in the for loop, loop from 0 to length-2... Or else the code will fill ArraymetDoubles from index 2 to two places outside the array 无论如何,在for循环中,循环从0到length-2 ...否则代码会将ArraymetDoubles从索引2填充到数组外部的两个位置
public void changeArray()
{
arrayMetDoubles = new Double[lines.length - 2];
for(int i = 0; i < arrayMetDoubles.length; i++)
{
arrayMetDoubles[i] = Double.parseDouble(lines[i + 2]);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.