0

Basically in my switch case, I'm trying to get it so if a String is typed in I want to return a message saying "Please enter an integer". I've tried using the try { } catch { } to do this but what happens is that in some of my switch-cases, I take in String inputs and after these String inputs have been taken in and a message printed out. It prints "Please enter an integer".

How can I fix this?

3
Contributors
6
Replies
9
Views
6 Years
Discussion Span
Last Post by gusano79
0

In general, using exceptions isn't an appropriate approach to this kind of situation. Please post your code; it will help us help you.

Edited by gusano79: rephrased

0

Ok, here's basically how my switch case works...

while (true) {
         System.out.println("Enter a number");
         int i = scan.nextInt();
         switch (i) {
            case 1: 
               String employee = scan.next();
               System.out.println(employee.alljobs);
               break;
            case 2: 
               String job = scan.next();
               System.out.println(job.allemployees);
               break;
         } 
      }
0

Ok, here's basically how my switch case works...

while (true) {
         System.out.println("Enter a number");
         int i = scan.nextInt();
         switch (i) {
            case 1: 
               String employee = scan.next();
               System.out.println(employee.alljobs);
               break;
            case 2: 
               String job = scan.next();
               System.out.println(job.allemployees);
               break;
         } 
      }

I was going to suggest that you read a string instead, then test it to see if it's an integer and take appropriate action based on that test, but I forgot that the Java library handles that by throwing exceptions itself... :/

The exception way:

System.out.println("Enter a number");
try
{
    int i = scan.nextInt();
    switch (i) {
        case 1: 
            String employee = scan.next();
            System.out.println(employee.alljobs);
            break;
        case 2: 
            String job = scan.next();
            System.out.println(job.allemployees);
            break;
    }
}
catch(InputMismatchException) {
    System.out.println("Please enter an integer");
}

Using Integer.parseInt would look very much the same.

I prefer to only use exceptions for truly unexpected input, and I wouldn't classify non-integer input here as 'exceptional'--users enter all sorts of weirdness.

If you'd like to try it without exceptions, you can read a string and use a regular expression to see if every character in the string is a digit, and optionally detect a +/- sign at the front. I'm not sure that this is necessarily better, though, in terms of readability.

0

I forgot to mention that I have all of this in a while loop saying
while (true) {
switch()....

}
}

So when I put your solution in and a non integer is typed in, it keeps printing the same thing over and over again since its looping over again and again

0

while(true) will always be true, so it will never leave the loop, unless you add a break statement, or change expression you are testing on

0

I forgot to mention that I have all of this in a while loop saying
while (true) {
switch()....

}
}

So when I put your solution in and a non integer is typed in, it keeps printing the same thing over and over again since its looping over again and again

Why do you need the while(true) ? Is it just there to re-ask if they don't enter an integer, or is there some other reason you need that loop?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.