-2

how can I give a condition on an input from "system.in" that will halt the program until the right value is inserted?

for exemple, I'm waiting for an INT from the user, 1,2,3,4 or 5 the user inputs "hello"

how can i give the user a message of "Invalid input, try again" and keep the program at halt until he does give the right one?

update: I didnt came so you can write my code, right now it looks something like this:

            int j=UserIn.nextInt();

            switch (j) {
            case 1:
                break;

            case 2:
                writetoDic(word, "dict.txt"); 
                break;

            case 3:
                word = correction;
                break;

i'm asking that, if im getiing something else than an int from the user, how can i ask the user to give a valid argument instead of just getting an error?

Paul Samsotha
  • 188,774
  • 31
  • 430
  • 651
Tzur
  • 3
  • 2
  • You won't receive any data unless the user presses `ENTER`. – Eng.Fouad Nov 16 '13 at 00:28
  • If you have tried something to achieve this then please share your code. – Smit Nov 16 '13 at 00:28
  • I didnt try, i have no idea how to write it. – Tzur Nov 16 '13 at 00:29
  • 2
    @Tzur Then first try something. Anything. If your attempt doesn't work and you don't know why, and you've tried everything in your power, then feel free to post that attempt (and what you tried) and look for help. Even a really bad implementation on your own is probably going to give you more in the long run than a solution copy-pasted from us. – Dennis Meng Nov 16 '13 at 00:31
  • @Tzur My friend, Nobody is going to write code for you. YOu have to try first and if you stumbled somewhere then ask for help. If you dont know how to write then go through [--> ***Java Tutorial*** – Smit Nov 16 '13 at 00:33
  • If you wan't to simplify the problem, you could just `catch` an `InputMistmatchException` and only need to condition to be and `int` in the range of two numbers. That's how I'd do it. – Paul Samsotha Nov 16 '13 at 00:40
  • You could, for an EVEN simpler (but less effective and not recommended, okay if you're an absolute beginner) method, just use a while loop where the condition would be, for example, "`while(j != 1 && j != 2 && j != 3...)`" – IHazABone Nov 16 '13 at 01:04
  • @IHazBone, what if input is a `String`? You would need to catch that exception. – Paul Samsotha Nov 16 '13 at 01:06

1 Answers1

0

You need to use a loop. I don't think you actually mean halt the program, but actually preventing to program to proceed until valid input. You can do something like this

Scanner scanner = new Scanner(System.in);
int num;

while (true) {
    try {
        System.out.println("Enter a number: ");
        num = scanner.nextInt();
        if (num >= 1 && num <= 5) {
            break;
        }
    } catch (InputMistmatchException ex){
        System.err.println("Input needs to be a number between 1 and 5, dummy.");
    }
}

Program will run if not between 1 and 5 and not an integer

Paul Samsotha
  • 188,774
  • 31
  • 430
  • 651
  • this helped a lot. though i ended in an endless loop so I had to add: scanner.nextline() and the program waited to the next input thanks a lot! – Tzur Nov 16 '13 at 01:16