I have this runtime error: Error:
Exception in thread "main" java.util.NoSuchElementException at java.base/java.util.Scanner.throwFor(Scanner.java:937) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at ArrayOperations.main(Main.java:31)
what should I repair in this code? :
import java.util.Scanner;
class ArrayOperations {
public static void reverseElements(int[][] twoDimArray) {
// write your code here
for (int i = 0; i < twoDimArray.length; i++) {
int left = 0;
int right = twoDimArray[i].length - 1;
while (left < right) {
int temp = twoDimArray[i][left];
twoDimArray[i][left] = twoDimArray[i][right];
twoDimArray[i][right] = temp;
left++;
right--;
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[][] twoDimArray = new int[3][4];
// System.out.println("Enter the elements of the twoDimArray:");
for (int i = 0; i < twoDimArray.length; i++) {
for (int j = 0; j < twoDimArray[i].length; j++) {
twoDimArray[i][j] = scanner.nextInt();
}
}
reverseElements(twoDimArray);
// System.out.println("Reversed twoDimArray:");
for (int[] row : twoDimArray) {
for (int element : row) {
System.out.print(element + " ");
}
System.out.println();
}
scanner.close();
}
}
I want resolve this runtime error because I have workout on Hyperskill. Program work correctly in my IntelliJ input and output are correctl but after checking IntelliJ with Hyperskill website show me this error. Thank you for your help and answer . input is this:
0 0 9 9
1 2 3 4
5 6 7 8
output is this:
9 9 0 0
4 3 2 1
8 7 6 5
For starters change your code like that and run it on the online platform:
for (int i = 0; i < twoDimArray.length; i++) {
for (int j = 0; j < twoDimArray[i].length; j++) {
System.out.println("Reading element for index [" + i + "][" + j + "]");
String debug = scanner.next();
// twoDimArray[i][j] = scanner.nextInt();
System.out.println("Read element for index [" + i + "][" + j + "] = " + debug);
}
}
Let us know what is the output.