-2

I'm trying to insert a do-while loop inside do-while loop to check if the input is integers. I added a comment where I want to insert the do-while loop. Everything works perfectly fine except for that part.

public static void main(String[] args){
    int num1, num2;
    char response;
    Scanner in = new Scanner(System.in);
    Scanner reader = new Scanner(System.in);

    do {    
        System.out.print("Enter number: ");
        num1 = in.nextInt();
        response = (char) reader.nextLine().charAt(0);

        //I want to check if the input is integer here
        do{
            System.out.println("Invalid input!\nPlease input integers only!");
        } while (response != /*something*/);

        for(num2=0; num2 < 11; num2++) {
            System.out.println(num1 + "X" + num2 + "=" + (num1 * num2));
            }

        do {
            System.out.println("\nDo you want to try again? [y/n]");
            response = (char) reader.nextLine().charAt(0);
            if (response != 'n'&& response != 'N' && response != 'Y' && response != 'y')
            System.out.println("Input invalid!");
        } while (response != 'n' && response != 'N' && response != 'y' && response != 'Y');

     } while (response != 'N' && response != 'n');{
         System.out.println("Thank you for using the table");}
}
Tobilogs
  • 1
  • 1
  • Welcome at Stack Overflow! Please consult https://stackoverflow.com/help/how-to-ask, in particular concentrate on relevant information (already edited by @StephenC). – Konrad Höffner Jun 22 '17 at 12:47
  • You can bold preformated code btw. The typical way to pointing to a certain line is to put a comment in the code that show where your talking about. – Carcigenicate Jun 22 '17 at 12:47
  • Integer.parseInt might be helpful here :) – Lemonov Jun 22 '17 at 12:48
  • Don't wrap more than one `Scanner` over the same source (in your case `System.in`). – Flown Jun 22 '17 at 12:49
  • @Flown You use 1 character indents? You monster! – Michael Jun 22 '17 at 12:49
  • @Flown - STOP messing up the formatting!!! – Stephen C Jun 22 '17 at 12:52
  • 1
    Please do a tiny bit of research the next time. This is a superbasic thing; and has been asked by zillions of newbies before. And answered. And again ... – GhostCat Jun 22 '17 at 12:52
  • @StephenC Sorry, but why spend time re-formatting this? It is an obvious DUP ... identify the DUP, vote to close, downvote; some time later vote for deletion, next ;-) – GhostCat Jun 22 '17 at 12:54
  • @Carcigenicate, I did and I just wanted to bold it. Thank for the reminder. – Tobilogs Jun 22 '17 at 12:56
  • @GhostCat, I did some research; However, I tried integrating others' code but it gave me more error. Sorry, beginner here. – Tobilogs Jun 22 '17 at 12:58
  • Hmm... You could use `Scanner.nextInt()`, maybe? I don't know why no one every wrote this... – Olivier Grégoire Jun 22 '17 at 13:08
  • @Tobilogs - a beginner should first learn to write his own code. From scratch. You should only "integrate" someone elses code into yours if you are a good enough programmer to be able to 1) decide if the code you are integrating is OK, and 2) understand how to make the code work with yours. – Stephen C Jun 22 '17 at 13:08
  • @StephenC - Thank you! I was actually trying to make my own code. It just happened that I haven't seen one similar to my program. I'll take note of that. Again, thanks! – Tobilogs Jun 22 '17 at 13:27

2 Answers2

1

Instead of using a do while you can check using the following way

try{
    yourNumber = Integer.parseInt(yourInput);
}catch (NumberFormatException ex) {
    //handle exception here
}
Rahul Singh
  • 19,030
  • 11
  • 64
  • 86
0

you can use "brute force" and try to parse users input, if something invalid is given then you get a NumberFormatException

    int num1 = 0;
    String intCandidate = null;
    boolean valid = false;
    Scanner in = new Scanner(System.in);

    while (!valid) {
        try {
            System.out.print("Enter number: ");
            intCandidate = in.nextLine();
            num1 = Integer.parseInt(intCandidate);
            valid = true;
        } catch (NumberFormatException e) {
            System.out.print("Nope... ");
        }
    }
    in.close();
    System.out.println("Thanks!");
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97