0

first post here. I was instructed to change my code to loop back to the beginning of the array and ask the user for input again after they input something invalid (Like 0 or 5 for example). If someone could point me in the right direction, I would be thankful.

package lepackage;
import java.util.Scanner;
public class SwitchItUp {
    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
       System.out.println("Enter menu item:");
       int input = scanner.nextInt();
        String inputString;
        switch (input) {
            case 1:  inputString = "User want to Enter data";
                     break;
            case 2:  inputString = "User want to Display sata";
                     break;
            case 3:  inputString = "User want to Print data";
                     break;
            case 4:  inputString = "User want to Exit";
                     break;
            default: inputString = "Invalid Number";
                     break;
        }
        System.out.println(inputString);
    }
}
Stylised
  • 33
  • 3
  • 4
    I point you in the direction of loops, e.g. the `while` loop. – khelwood Nov 22 '17 at 09:03
  • There are pretty much two ways to achive this, by using recursion or by using an actual loop. As a sidenote i don´t see an array that you did mention. – SomeJavaGuy Nov 22 '17 at 09:06

2 Answers2

3

I'd surround it with a do...while loop

do {
//your code here
} while (!(input > 0 && input < 5));

See it online!

achAmháin
  • 4,176
  • 4
  • 17
  • 40
  • You have added reverse condition, it should be `while (input > 0 && input < 5);` – hiren Nov 22 '17 at 09:57
  • @HirenPandit - to make it clearer, it's just the opposite of `while (input < 0 || input > 5);` – achAmháin Nov 22 '17 at 10:06
  • What I understood, user want to exit the loop when input is NOT (1, 2, 3, or 4) according to your condition loop will be executed when input is less than 0 or greater than 5. Am I missing something ? – hiren Nov 22 '17 at 10:10
  • Best explanation I can do from my phone - user enters 55, gets to `while` which says *is the number greater than 0 and less than 5* - answer 'no', but obviously we're using the opposite of that, so answer is 'yes', and loop continues. – achAmháin Nov 22 '17 at 10:15
  • Exactly loop will continue for value 55 but we want to break because it is not one of expected option. – hiren Nov 22 '17 at 10:19
  • 1
    Ok I read question again and understood user want to loop again after invalid input.. sorry for trouble :) – hiren Nov 22 '17 at 10:22
1

how about using a label here. though It's not the cleaner approach compare to do.. while. see the code . Also don't forget to close the scanner !!

 Scanner scanner = new Scanner(System.in);
        int input;
        badinput: while (true) {
            System.out.println("Enter menu item:");
            input = scanner.nextInt();
            String inputString;
            if ((!(input > 0 && input < 5)))
                continue badinput;
            else {
                switch (input) {
                //switch case
                }
                System.out.println(inputString);
                break;
            }

        }
        scanner.close();
Santanu Sahoo
  • 1,137
  • 11
  • 29