0

When I run this code I get this code in coderunner (an app that submits code for schoolwork)

    Scanner scan = new Scanner(System.in);

    double maxn = -90;
    double maxs = 90;
    double maxe = 180;
    double maxw = -180;
    double lat = 0;
    double longa = 0;
    int x = 1;

    while (x != 0) {
        System.out.println("Please enter a latitude:");
        lat = scan.nextDouble();
        if (lat >= maxn && lat <= 90)
            maxn = lat;
        if (lat <= maxs && lat >= -90)
            maxs = lat;

        System.out.println("Please enter a longitude:");
        longa = scan.nextDouble();
        if (longa <= maxe && longa >= -180)
            maxe = longa;
        if (longa >= maxw && longa <= 180)
            maxw = longa;

        System.out.println("Would you like to enter another location?");
        x = scan.nextInt();

    }
    System.out.println("Farthest North: " + maxn + "\nFarthest South: " + maxs + "\nFarthest East: " + maxe + "\nFarthest West: " + maxw);

I get the following error:

    Runtime Error

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Lesson_20_Activity.main(Main.java:315)
at Ideone.assertRegex(Main.java:85)
at Ideone.assertRegex(Main.java:76)
at Ideone.test(Main.java:40)
at Ideone.main(Main.java:29)

I have no idea how this error works, as I am new to coding. Can someone explain what this means and how to fix it?

EDIT: My input is

Please enter the latitude: 41.678 Please enter the longitude: 69.938 Would you like to enter another location? 1 Please enter the latitude: 41.755 Please enter the longitude: 69.862 Would you like to enter another location? 1 Please enter the latitude: 41.829 Please enter the longitude: 69.947 Would you like to enter another location? 1 Please enter the latitude: 300 Please enter the longitude: 69.947 Incorrect Latitude or Longitude Please enter the latitude: 41.827 Please enter the longitude: 69.904 Would you like to enter another location? 0 Farthest North: 41.829 Farthest South: 41.678 Farthest East: 69.947 Farthest West: 69.862

Also, I have tried changing x to a double and a string input, with no success. The error I got for each is NoSuchElementError and NoSuchLineError (respectively)

3 Answers3

1

From Javadoc, InputMismatchException is thrown when:

Thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type.

In your code, you are calling scan.nextInt() and scan.nextDouble(). Make sure that you are only passing valid int and double values respectively to each of these calls. That is, inputting a double value when the Scanner is expecting an int value (scan.nextInt()) would throw the above error.

Rishikesh Dhokare
  • 3,559
  • 23
  • 34
curlyBraces
  • 1,095
  • 8
  • 12
0

Note that when you don't want a line break you should them use the System.out.print then write '\r\n'.

I have run your code and got the right output, but you have to notice what input are you giving to your command line. I'd read all Strings and then convert to the desired primitive format to avoid this error.

0

After you enter longitude and latitude, you are asking for the question where you try again.

x= nextInt()

Here, You are accepting an int (0 to close the while loop, not otherwise). If you type any string or double value there, it will cause Inputmismtach exception. I tried your code, and it works fine if those integers and decimals are given correcly as shown below.

Please enter a latitude:
12.3
Please enter a longitude:
14.3
Would you like to enter another location?
1
Please enter a latitude:
12.3
Please enter a longitude:
14.5
Would you like to enter another location?
0
Farthest North: 12.3
Farthest South: 12.3
Farthest East: 14.3
Farthest West: 14.5
Gimhani
  • 1,318
  • 13
  • 23