int [][] matrix = new int [9][9]; Scanner sc = new Scanner(System.in); System.ou
ID: 3723165 • Letter: I
Question
int [][] matrix = new int [9][9];
Scanner sc = new Scanner(System.in);
System.out.print("Enter file name ");
String fileName = sc.next();
Scanner input = new Scanner (fileName);
while (input.hasNext()){
for (int col = 0; col < 9 ; col ++){
for (int row = 0; row < 9 ; col ++){
sudoku [col][row] = input.nextInt();
System.out.println(matrix [col][row]);
}
}
}
}
i got error this
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Sudoku.main(matrix.java:30)
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Explanation / Answer
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.util.Scanner; public class Mat { public static void main(String[] args) throws FileNotFoundException { //encounters filenotfoundexception int [][] matrix = new int [9][9]; Scanner sc = new Scanner(System.in); System.out.print("Enter file name "); String filename = "F:\pro\Intellij\src\" + sc.next(); //getting filename from user and appending to it absolute path FileReader fileReader = new FileReader(filename); BufferedReader bufferedReader = new BufferedReader(fileReader); Scanner input = new Scanner (bufferedReader); while (input.hasNext()){ for (int col = 0; col < 9 ; col ++){ for (int row = 0; row < 9 ; row ++){ matrix[col][row] = input.nextInt(); System.out.println(matrix[col][row]); } } } } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.