0

The code I've written to scan in a 2d array can be seen below, im having runtime issues this is the output:

Runtime Error

Exception in thread "main" java.util.InputMismatchException: For input string: "00000000100000000000"

at java.util.Scanner.nextInt(Scanner.java:2123)

at java.util.Scanner.nextInt(Scanner.java:2076)

at Main.main(Main.java:17)

Code:

import java.util.Scanner;

public class Main 
{
    public static void main(String args[]) 
    {
        final int m = 20; // Rows
        final int n = 20; // Columns
        Scanner s1 = new Scanner(System.in);
        int num = s1.nextInt();
        int myArray1[][] = new int[m][n]; 

        for (int i = 0; i < m; i++) 
        {
            for (int j = 0; j < n; j++) 
            {
                myArray1[i][j] = s1.nextInt();
            }
        }

        for (int i = 0; i < m; i++) 
        {
            for (int j = 0; j < n; j++) 
            {
                System.out.print(myArray1[i][j] + "");
            }
        }
    }
}

If someone could help me out i'd really appreciate it!

this input is:

00000000000000000000
00000000000000000000
00000000010000000000
00000000010000000000
00000000000000000000
00000000000000000000
1
  • 2
    Java is to Javascript as Pain is to Painting, or Ham is to Hamster. They are completely different. It is highly recommended that aspiring coders try to learn the name of the language they're attempting to write code in. When you post a question, please tag it appropriately - this lets those with knowledge of the language you need help with to see your question. Commented May 8, 2020 at 8:14

1 Answer 1

1

You are not using Scanner in the right way.

Scanner.nextInt() scans the next token of the input as an int, not singel nums. So every line of your input would be scanned as a number. The line 00000000000000000000 would be scanned as 0, and the line 00000000010000000000 is where the exception occured because 10000000000 has exceeded the max value of Integer, which is 2147483647.

You can divide your input lines into tokens by space.

A better solution is read lines as string then split the string into numbers. Input exit in a new line to stop your scan.

int curRow = 0;
while (true) {
    String line = s1.nextLine();
    if ("exit".equals(line)) {
        break;
    } else {
        // assume there are n numbers in range 0 - 9 for each line you input
        char[] numArray = line.toCharArray();
        for (int i = 0; i < n; i++) {
            myArray1[curRow][i] = Integer.parseInt(String.valueOf(numArray[i]));
        }
        curRow++;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the help i understand whats happening now, i implemented the suggestions but im still getting the runtime error code: prnt.sc/sd3r8n error: prnt.sc/sd3riy
@andreimad I cant access your picture so I dont know what the error is. Maybe your input is not exactly the same with what I used for test. As long as you know whats wrong and what you can try, you can find your solution that fits your input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.