The assignment I have is to make four on a row, and this step asks a user for the colon on which a new chip is inserted and I have to tell the user on which row the chip ends.
input:
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 1 1 0 0 0
2 2 2 1 2 2 2
1 2 1 1 1 2 1
1 2 1 2 1 2 1
4
and the output should be:
Row: 5
we are given a basic code, see below, but if I run this I first get the error:
Compilation error
Main.java:14: error: cannot find symbol
bord[j][i] = scanner.nextInt();
^
symbol: variable scanner
location: class Main
1 error
and to solve this I added "Scanner scanner = new Scanner(System.in);" to leesinbord(), but then I get the 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 Main.main(Main.java:27)
and I think it is because I do "Scanner scanner = new Scanner(System.in);" twice, but I don’t know for sure or how to solve this.
import java.util.Scanner;
public class Main {
public static int[][] bord = new int[7][6];
public static int getVakje(int x, int y){
return bord[x-1][y-1];
}
public static void setVakje(int x, int y, int inhoud){
bord[x-1][y-1] = inhoud;
}
public static void leesinbord(){
//Scanner scanner = new Scanner(System.in);
for (int i = 5; i >=0;i--){
for (int j = 0; j < 7; j++){
bord[j][i] = scanner.nextInt();
}
}
}
public static void printrij(int kolom) {
// Voer hieronder de code in voor de methode printrij
// Voer hierboven de code in voor de methode printrij
}
public static void main(String[] args) {
leesinbord();
Scanner scanner = new Scanner(System.in);
int kolom = scanner.nextInt();
//printrij(kolom);
}
}
>Solution :
as scanner declared in main cannot be accessed in method
Try passing scanner to leesinbord() method like –
public static void leesinbord(Scanner scanner)
and make scanner static like –
static Scanner scanner = new Scanner(System.in);