0
votes

My program takes in integer input, then a bigger integer input, then a string.

"Enter a number: "

"Enter a bigger number: "

"Enter a string: "

I was trying to use a try/catch to catch cases if a string was input instead of an integer. I sort of got the code to work, but after the error message executes it skips over the next int input question and goes straight to, "Enter a string."

I assume I need a while loop somewhere, but I'm having trouble figuring out where exactly to place it, and what argument to use. Simply put, (I think) I need: while (input != string). I just have no idea how to do that.

try {
            System.out.println("Enter a number: ");

            int start = myscanner.nextInt();

            System.out.println("Enter a bigger number: ");

            int end = myscanner.nextInt();

            if (start > end) {
                System.out.println("Error: Start should be smaller.");
            } else {

                int result = sumInt(start, end);
                System.out.println("Result: " + result);
            }
        } catch (InputMismatchException exception) {
            System.out.println("Enter a number, not a string.");

        }
```````````````````````````````````````````````````````````````````````````
1
Have you tried using do-while? Please refer to this link: docs.oracle.com/javase/tutorial/java/nutsandbolts/while.htmlDhruv Patel
I have tried do-while. To be honest, I'm stumped. Wherever I place the loop I get errors through the rest of my code below this. Maybe I'm just taking the wrong approach altogether. All I wanted was something to catch errors if the wrong data type was input. Thanks for your help.Devin
Maybe you can use something like this: if (row start!= int) { throw new IllegalArgumentException("Please enter integer."); }Dhruv Patel
I tried: while (throw start != int) and got "illegal start of expression" and ".class expected". I can't seem to make that one work.Devin
you cannot have throw inside a conditional. Has to be something as follows: while(start != int) { throw new IllegalArgumentException("Please enter an integer"): }Dhruv Patel

1 Answers

0
votes
import java.util.Scanner;
public class Main
{
    public static void main(String[] args) {
       try{
            Scanner myscanner = new Scanner(System.in); 
            int option =1;
            do{
               System.out.println("Enter a number: ");
               String start = myscanner.next();
               boolean sflag= isInteger(start);
               if(!sflag )
               {System.out.println("Error: value not Integer.");  continue;}
               System.out.println("Enter a bigger number: ");
               String end = myscanner.next();
               boolean eflag=  isInteger(end);
              if( !eflag)
                {System.out.println("Error: value not Integer.");  continue;}
              if ( Integer.parseInt(start) > Integer.parseInt(  end)) {
                 System.out.println("Error: Start should be smaller.");
              } else {
                 int result = sumInt( Integer.parseInt( start),  
                 Integer.parseInt( end));
                 System.out.println("Result: " + result);
                 System.out.println("select following option");
                 System.out.println("1. continue");
                 System.out.println("2.exit");
                 option = myscanner.nextInt();
                  //isInteger(option);
             }

        }while(option != 2);

     }catch(Exception e){
        System.out.println(e);
    }
}
public static boolean isInteger( String input ) {
try {
  Integer.parseInt( input );
  return true;
 }
  catch( Exception e ) {
  return false;
 }
 }
      public static Integer sumInt(int start, int end){
      return start + end;
   }
}