0

So I am making a program that will output the greatest and least greatest values of the users' input of longitude and latitude. Interestingly enough, the program actaully runs and works! But it keeps getting an error as such:

    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:328)
    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 a feeling it is something to do with Scanner class, but I've checked to see if everything is right at least a dozen times...

    import java.util.Scanner;
    import java.lang.Math;

    class Lesson_20_Activity {
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);

        double lat=0;
        double lon=0;
        int num = 1;
        //min variables
        double xlat=Integer.MAX_VALUE;
        double xlon=Integer.MAX_VALUE; 
        //max variables
        double ylat=Integer.MIN_VALUE;
        double ylon=Integer.MIN_VALUE;    

       /* the program wants the greatest and lowest values entered in latitude longitude.
       */ 
        while (num == 1){
            System.out.println("Please enter the latitude: ");
            lat = scan.nextDouble();
            //establishes the largest input
            if(lat>-90&&lat<90&&lat>ylat){
                ylat=lat;
            } 
            //establishes the smallest input
            if(lat>-90&&lat<90&&lat<xlat){
                xlat=lat;
            } 

            System.out.println("Please enter the longitude: ");
            lon = scan.nextDouble();
            //establishes the largest input
            if(lon>-180&&lon<180&&lon>ylon){
                ylon=lon;
            } 
            //establishes the smallest input
            if(lon>-180&&lon<180&&lon<xlon){
                xlon=lon;
            } 

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

        System.out.println("Farthest North: " + ylat);
        System.out.println("Farthest South: " + xlat);
        System.out.println("Farthest East: " + ylon);
        System.out.println("Farthest West: " + xlon);

    }
}
  • Can you give examples of inputs that cause the error? – ChiefTwoPencils Oct 13 '19 at 20:27
  • From the call stack I can confirm your suspicion that it has something to do with the Scanner class: `at java.util.Scanner.nextInt(Scanner.java:2117)`. It wants to parse an int but can't. To debug it I'd propose to wrap the scanner calls in separate methods with try-catch blocks, set a breakpoint in the catch expression and check out what's going on. See [debugging](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems?noredirect=1&lq=1), a cool thing. – Curiosa Globunznik Oct 13 '19 at 20:30
  • Also, look into line 328. `at Lesson_20_Activity.main(Main.java:328)` – ChiefTwoPencils Oct 13 '19 at 20:34

0 Answers0