0

Im creating a lift simulation but am having trouble with the error handling. When a user inputs a letter instead of a integer it throws a InputMismatchException. How do i fix this? Here is the code below:

System.out.println("Please define... ");
        System.out.println("Number of floors:");
        Building building = new Building(in.nextInt()); 
        System.out.println("Number of lifts:");
        building.setNoOfLifts(in.nextInt());
        building.setLifts();
        System.out.println("Maximum number of people to enter building at one time:");
        building.setMaxPeople(in.nextInt());
        System.out.println("Maximum capacity of the lifts:");
        int maxLiftCapacity = in.nextInt();
        System.out.println("Simulation will now begin.");
        Queue queue = new Queue();
5
  • When a user inputs a letter instead of a integer it throws a InputMismatchException. How do i fix this? You could cuss him out or threaten dire consequences. But you may just want to use try { ...} catch(InputMismatchException e) { ...} around your code. Commented Apr 18, 2014 at 23:19
  • yes i have done this but it still comes up with the same error Commented Apr 18, 2014 at 23:20
  • If the user enters a non-integer, then you're going to get an exception. What exactly are you trying to fix? Do you just want to loop, asking for an int, until the user enters an actual integer? Commented Apr 18, 2014 at 23:23
  • Exception in thread "main" 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 lift_simulator.LiftSimulator.main(LiftSimulator.java:21) Commented Apr 18, 2014 at 23:24
  • yes loop until the user enters an integer Commented Apr 18, 2014 at 23:25

2 Answers 2

2

Here's some code that may help a bit. Read the integer and catch the exception in a loop. When the input is an integer, the loop exits. When an exception is thrown, the loop continues.

  Scanner sc = new Scanner(System.in);
  int i=0;
  while(true) {
      System.out.println("enter an integer:");
      try {
          int i = sc.nextInt();          
          break;
     } catch(InputMismatchException ignore){}
 }

 // i has been read successfully
Sign up to request clarification or add additional context in comments.

Comments

0

nextInt() is only applicable if you know for certain that you will get an integral value next. (That is, the input is from a source more reliable than a user.)

What do I suggest? The broadest scope of input is a String, so take one of those. That might look something like the below.

String strFloors = in.nextLine();
strFloors = strFloors.replaceAll("[^\\d]", ""); // Strip non 1234567890 characters
intFloors = Integer.valueOf(strFloors);
Building building = new Building(intFloors);

Or if you want to fit it into just the one line, you could put it together like

Building building = new Building(Integer.valueOf(in.nextLine().replaceAll("[^\\d]", "")));

The best error handling is certainty of none arising.

Comments

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.