I can print out the index lineArr[1], but when I try to initialize x with the value it gives me an ArrayIndexOutOfBoundException. On the file it is the line that says y=parseDouble(lineArr[1]);
else {
System.out.println("Enter file name: ");
String fileName = keyboard.next();
Scanner sc = new Scanner(new FileReader(fileName));
double x,y,z,b;
for (int i = 0; i < numLinEq; i++) {
String line = sc.nextLine();
String[] lineArr = line.split(" ");
for(int j=0;j< lineArr.length;j++){
System.out.println(lineArr[j]);
}
x=parseDouble(lineArr[0]);
y=parseDouble(lineArr[1]);
z=parseDouble(lineArr[2]);
b=parseDouble(lineArr[3]);
xyzArr[i][0] = parseDouble(lineArr[0]);
xyzArr[i][1] = parseDouble(lineArr[1]);
xyzArr[i][2] = parseDouble(lineArr[2]);
bArr[i] = parseDouble(lineArr[3]);
}
}
double[] answerArr = gaussianElimSolve(xyzArr, bArr);
System.out.println("x = " + Math.round(answerArr[0]) + "\ny = " + Math.round(answerArr[1])
+ "\nz = " + Math.round(answerArr[2]));
}
private static double parseDouble(String str) {
if (str == null || str.isEmpty()) {
return 0.0;
} else {
return Double.parseDouble(str);
}
}
OUTPUT:
How many linear equations do you need me to solve?:
3
Please choose from the following:
Input 1 if you want to manually input the coefficient values:
Input 2 if you have a file with the coefficient values:
2
Enter file name:
file.txt
2
3
0
8
After this it gives me an ArrayOutOfBoundException