Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8456569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:34:39+00:00 2026-06-10T12:34:39+00:00

I keep getting: java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:909) at java.util.Scanner.next(Scanner.java:1530) at java.util.Scanner.nextInt(Scanner.java:2160) at java.util.Scanner.nextInt(Scanner.java:2119) at Driver0.readFile(Driver0.java:38)

  • 0

I keep getting:

java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at Driver0.readFile(Driver0.java:38)
at Driver0.main(Driver0.java:18)

Trying to use the scanner class since it’s all I know so far. any help appreciated. Trying to read the 2d array but it never reads it fully. My input.txt file is:

3 5

2 3 4 5 10

4 5 2 3 7

-3 -1 0 1 5

import java.io.*;
import java.util.*;
public class Driver0 {
//public static String fileName; // declare so it may be used outside of main
public static int[][] array; // declare as empty until given dimensions
public static int dimensionA, dimensionB; // dimensions used in methods
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("What is the name of the data file?");
    System.out.print("> ");
    String fileName = input.nextLine();
    readFile(fileName);
    String nextCommand = "";
    while (!(nextCommand.equals("quit"))) {
        System.out.println("\nNext command");
        System.out.print("> ");
        nextCommand = input.nextLine();
        choice (nextCommand);
    }
}
public static void choice(String command) {
    switch (command) {
        case "help": System.out.println("show array\nrows sorted\ncols sorted"+
                    "increase row i by n\nincrease col j by n\nquit\n");
                break;
        default: showArray();
    }
}
public static void readFile(String fileName) {
    try {
        Scanner foo = new Scanner(fileName);
        dimensionA = foo.nextInt();
        dimensionB = foo.nextInt();

        array = new int[dimensionA][dimensionB];
        while (foo.hasNext()) {
            for (int row = 0; row < dimensionA; row++) {
                foo.nextLine();
                for (int column = 0; column < dimensionB; column++) {
                    array[row][column] = foo.nextInt();
                }
            }
        }
        foo.close();
    }
    catch(Exception e) {
        e.printStackTrace(); // displays type of error
    }
}
public static void showArray() {
    for (int ro = 0; ro < dimensionA; ro++) {
        for (int col = 0; col < dimensionB; col++) {
            System.out.print(array[ro][col]);
        }
        System.out.println();
    }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T12:34:40+00:00Added an answer on June 10, 2026 at 12:34 pm

    Go through methods in the Scanner class.

    Specially, the hasXXX() and nextXXX() methods, they come in pairs. Try not to mix-match those methods which will make things complicated. You are using nextInt(), hasNext(), and nextLine(). The docs also explains what these methods do when you invoke them.

    For your case, the methods hasNextLine() and nextLine() will be enough if you are willing to use the String.split(). You can split the string returned by nextLine() and parse the integer and assign it to the array element.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Getting error with the following code: import java.util.Scanner; public class FahrenheitToCelsius{ public static void
This is the error I keep getting. Exception in thread Thread-3 java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:782)
I keep getting 21002 'java.lang.NullPointerException' errors from Apple when I try to test my
I keep getting this java.lang.NullPointerException on the line where mLoginButton's onClickListener is initiated. I
import java.util.Scanner; public class EP55Out { public static void main(String[] args) { Scanner in
I keep getting a ClassCastException Exception in thread main java.lang.ClassCastException: java.lang.String cannot be cast
I keep getting these errors while retreiving images here. 07-26 17:21:29.194: ERROR/AndroidRuntime(396): java.lang.RuntimeException: An
I'm trying to write an SAX XML Parser in Java and I keep getting
Keep getting this error after inserting a subdatasheet into a query and trying to
Keep getting the error Arguments are not sufficiently instantiated for the multiplication by addition

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.