0

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

Mark Rotteveel
  • 82,132
  • 136
  • 114
  • 158
D.Park
  • 1
  • 1
  • You can't, you are likely mistaken, because you're loop will only print `lineArr[1]` when it exists. As it doesn't exist, it isn't printed, but your attempt to access it explicitly will fail because it doesn't exist. – Mark Rotteveel Sep 19 '20 at 06:59
  • 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 – D.Park Sep 19 '20 at 07:49
  • This is the output I am getting, so it is printing, but after this prints it gives me an error – D.Park Sep 19 '20 at 07:49
  • Please provide a [mre], preferably without file input. – Mark Rotteveel Sep 19 '20 at 09:24

0 Answers0