-3
import java.util.Scanner;

public class test {

/**
 * @param args
 */
public static void main(String[] args) 
{
    Scanner input = new Scanner (System.in);
    boolean US1 = false;
    boolean game;
    int score = 1;
    int wage = 0;   
    int fin_score = 0;
    String ans;

    if (US1 == false) {
        game = false;
        System.out.println (score);
        System.out.println("Enter a wager");
        wage =  input.nextInt();
    }

    if (wage < score) {
        System.out.println ("What is the capital of Liberia?");
        ans = input.next();

        if (ans.equalsIgnoreCase("Monrovia")) {
            System.out.println ("You got it right!");
            System.out.println ("Final score " + fin_score);
        }
    }
}
}

I have found a bunch of solutions using InputMismatchException and try{}catch{} but they never work when they are implemented in my code. is there a way to implement these here? I am trying to make a loop that iterates until the wage entered is an integer

Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
  • Of course such an approach (with try/catch) works. Thus the (not shown) implementation must have been at fault. – user2864740 Jun 13 '14 at 00:58
  • What have you tried? Using the solutions online will work in this case, I guarantee it. – nook Jun 13 '14 at 00:58

3 Answers3

0

You can have multiple catch exceptions in your code to check for bad input. For example

try{

    wage = input.nextInt();

catch (InputMismatchException e){ 
   System.out.print(e.getMessage());
   //handle mismatch input exception
}

catch (NumberFormatException e) {
    System.out.print(e.getMessage());
    //handle NFE 
}

catch (Exception e) {
    System.out.print(e.getMessage());
    //last ditch case
}

Any of these would work fine for Scanner errors, but InputMismatchException is the best to use. It would help your case a great deal if you included the non-working code with the try-catch blocks.

Jason
  • 11,263
  • 21
  • 87
  • 181
  • 1
    Doesn't answer the question... "I am trying to make a loop that iterates until the wage entered is an integer". Also, nextInt is bad for stdin, it doesn't look at the whole line to see that it's valid, and it leaves any other words/integers on the line. –  Jun 13 '14 at 01:08
0

First of all, You should be using Scanner.nextLine, because Scanner.nextInt uses spaces and newlines as delimiters, which is probably not what you want (any thing after a space will be left on the scanner, breaking any next reads).

Try this instead:

boolean valid = false;
System.out.print("Enter a wager: "); //Looks nicer when the input is put right next to the label
while(!valid)
    try {
        wage = Integer.valueOf(input.nextLine());
        valid = true;
    } catch (NumberFormatException e) {
        System.out.print("That's not a valid number! Enter a wager: ");
    }
}
  • where exactly would I place this in my code? it works but when I enter an integer it crashes – user3734973 Jun 13 '14 at 01:26
  • @user3734973: Replace `System.out.println("Enter a wager ");` and `wage = input.nextInt();` by what I gave you. Also, if your program makes java **crash**, then I suggest you reinstall it. If it just gives you an exception, then paste your exception! –  Jun 13 '14 at 21:50
0

Yes! There is a good way to do this:

Scanner input = new Scanner(System.in);
    boolean gotAnInt = false;
    while(!gotAnInt){
        System.out.println("Enter int: ");
        if(input.hasNextInt()){
            int theInt = input.nextInt();
            gotAnInt = true;
        }else{
            input.next();
        }

    }
Dean Leitersdorf
  • 1,303
  • 1
  • 11
  • 22