0

This is for an online assignment. The goal is to accept two inputs (in this code, the x and y variables) multiple times and then print the largest and smallest value of each variable. Here is the code:

import java.io.*;
import static java.lang.System.*;

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


class Lesson_20_Activity_One{

     public static void main (String str[]) throws IOException {
          Scanner scan = new Scanner(System.in);
          double x; //the latitude input
          double y; //the longitude input
          double n = -90; //the maximum north value
          double e = -180; //the maximum east value
          double s = 90; //the maximum south value
          double w = 180; //the maximum west value
          int escape = 1; //the value that dictates when it's time to exit the while loop
          while (escape == 1) {
          System.out.println("Please enter the latitude:");
          x = scan.nextDouble();
          System.out.println("Please enter the longitude:");
          y = scan.nextDouble();
          if ((x>n) && (x<=90))
              n = x;
          if ((x<s) && (x>=-90))
              s = x;
          if ((y>e) && (y<=180))
              e = y;
          if ((y<w) && (y>=-180))
              w = y;
          if ((x>90)||(x<-90)||(y>180)||(y<-180))
              System.out.println("Incorrect Latitude or Longitude");
          System.out.println("Would you like to enter another location?");
          escape = scan.nextInt();
          }
          System.out.println("Farthest North: " + n);
          System.out.println("Farthest South: " + s);
          System.out.println("Farthest East: " + e);
          System.out.println("Farthest West: " + w);
     }

}

This code runs exactly as intended on my personal compiler, Eclipse. However, when running this same program on the course's online compiler, I am met with these errors:

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:322)
    at Ideone.assertRegex(Main.java:85)
    at Ideone.assertRegex(Main.java:76)
    at Ideone.test(Main.java:40)
    at Ideone.main(Main.java:29)

What can I do to solve these errors? It seems it's a problem with my Scanner, but I don't know how to fix it.

user2709168
  • 117
  • 1
  • 14

2 Answers2

2

The error trace seems to indicate that the InputMismatchException is thrown when scan.nextInt() is called.

After your print System.out.println("Would you like to enter another location?");, if the user inputs anything other than an integer, scan.nextInt() will throw an exception. Even if you read the user's input as a string as suggested by someone else, the parseInt method in escape = Integer.parseInt(scan.nextLine()) will throw an error because the string may not be a valid integer.

I suggest adding try-catch block as follows around the scan.nextInt() call so that your program doesn't crash when a user inputs something other than a valid integer (like the number 8.238).

boolean inputValid = false;
System.out.println("Would you like to enter another location?");
while (!inputValid) {
    try {
        escape = scan.nextInt();
        inputValid = true;
    } catch (InputMismatchException e) {
        inputValid = false;
        System.out.println("Please enter a valid Integer");
    }
}

This code will continue asking the user until the user enters a valid integer.

  • I'm not sure how to implement what you're suggesting, sorry. I replaced the escape=scan.nextInt(); with the block you wrote and I'm met with `Main.java:323: error: cannot find symbol } catch (InputMismatchException e) { ^ symbol: class InputMismatchException location: class Lesson_20_Activity Main.java:323: error: variable e is already defined in method main(String[]) } catch (InputMismatchException e) {` – user2709168 Oct 14 '16 at 02:24
  • Ah, it may be because you've already defined a variable of double named "e" above in your code. Try renaming the `InputMismatchException e` to `InputMismatchException exception`, for example. – Shrivathsav Seshan Oct 16 '16 at 16:21
1

It might be because the online scanner's implementation of nextInt() is different and is causing the problems.

Perhaps trying to parse the input as a string and the as a double/int would work:

Scanner scan = new Scanner(System.in);
double x; // the latitude input
double y; // the longitude input
double n = -90; // the maximum north value
double e = -180; // the maximum east value
double s = 90; // the maximum south value
double w = 180; // the maximum west value
int escape = 1; // the value that dictates when it's time to exit the
// while loop
while (escape == 1) {
    System.out.println("Please enter the latitude:");
    x = Double.parseDouble(scan.nextLine());
    System.out.println("Please enter the longitude:");
    y = Double.parseDouble(scan.nextLine());
    if ((x > n) && (x <= 90))
        n = x;
    if ((x < s) && (x >= -90))
        s = x;
    if ((y > e) && (y <= 180))
        e = y;
    if ((y < w) && (y >= -180))
        w = y;
    if ((x > 90) || (x < -90) || (y > 180) || (y < -180))
        System.out.println("Incorrect Latitude or Longitude");
    System.out.println("Would you like to enter another location?");
    escape = Integer.parseInt(scan.nextLine());
        }
System.out.println("Farthest North: " + n);
System.out.println("Farthest South: " + s);
System.out.println("Farthest East: " + e);
System.out.println("Farthest West: " + w);

Again, this depends on the online scanner's way of doing things.

ItamarG3
  • 4,092
  • 6
  • 31
  • 44
  • Adding that code returns this: ' Exception in thread "main" java.lang.NumberFormatException: For input string: "41.827" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.parseInt(Integer.java:615) at Lesson_20_Activity.main(Main.java:323) at Ideone.assertRegex(Main.java:85) at Ideone.assertRegex(Main.java:76) at Ideone.test(Main.java:40)' – user2709168 Oct 14 '16 at 02:08
  • well that happens when you pass a double for escape – ItamarG3 Oct 14 '16 at 06:55