Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Exeption with four on a row

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading