1

When I enter my choice (1-4) for the loop, I have to press enter twice in order to invoke one of the cases.

I would like to fix it so that I can only press enter once, after typing my choice.

    Scanner scan = new Scanner(System.in);
    int choice = 0;
    while(true) {
        choice = scan.nextInt();
        switch(choice) {
            case 1:
                System.out.println("============================================================================");
                System.out.println("Name                Food Group          Calories            Daily percentage   ");
                System.out.println("============================================================================");

                x.roll();
                break;
            case 2:
                String foodname[] = new String[3];
                int caloriesum = 0;
                double dailypercentage = 0;
                int i = 0;
                while(i < 3) {
                    boolean flag = false;
                    System.out.print("Enter food name: ");
                    foodname[i] = scan.next();
                    for(int k = 0; k < x.getCurrentSize(); k++) {
                        if(foodname[i].equals(x.getAtIndex(k).getName())) {
                            i++;
                            flag = true;
                            caloriesum = caloriesum + x.getAtIndex(k).getCalories();
                            dailypercentage = dailypercentage + x.getAtIndex(k).getPercentage();
                            break;
                        }
                    }
                    if(flag == false) {
                        System.out.println("Food " + foodname[i] + " not in database, try again");
                    }
                }

                System.out.println("===============================");
                System.out.println("Your selected meal");
                System.out.println("===============================");
                System.out.println("Foods: " + foodname[0] + " " + foodname[1] + " " + foodname[2]);
                System.out.println("Total calorie count: " + caloriesum);
                System.out.println("Total daily percentage: " + dailypercentage);
                System.out.println("===============================");
                break;
            case 3:

                int num1, num2, num3;

                while(true) {
                    Random a = new Random();
                    num1 = a.nextInt(x.getCurrentSize());
                    Random b = new Random();
                    num2 = b.nextInt(x.getCurrentSize());
                    Random c = new Random();
                    num3 = c.nextInt(x.getCurrentSize());

                    if(num1 != num2 && num1 != num3 && num2 != num3) {
                        break;
                    }
                }

                System.out.println("===============================");
                System.out.println("Your selected meal");
                System.out.println("===============================");
                System.out.println("Foods: " + x.getAtIndex(num1).getName() + " " + x.getAtIndex(num2).getName() + " " + x.getAtIndex(num3).getName());
                System.out.println("Total calorie count: " + (x.getAtIndex(num1).getCalories() + x.getAtIndex(num2).getCalories() 
                        + x.getAtIndex(num3).getCalories()));
                System.out.println("Total daily percentage: " + (x.getAtIndex(num1).getPercentage() + x.getAtIndex(num2).getPercentage() 
                        + x.getAtIndex(num3).getPercentage()));
                System.out.println("===============================");
                break;
            case 4:
                System.out.print("Enter calorie limit: ");
                int calorielimit = scan.nextInt();
                for(int index = 0; index < x.getCurrentSize(); index++) {
                    if(x.getAtIndex(index).getCalories() > calorielimit) {
                        x.removeAtIndex(index);
                        index--;
                    }
                }
                System.out.println("============================================================================");
                System.out.println("Name                Food Group          Calories            Daily percentage   ");
                System.out.println("============================================================================");
                x.roll();
                break;
            default:
                scan.close();
                System.out.println("Done");
                return;
        }
    }

UPDATE

I added the code that was in the cases, if that helps. using nextLine or parseInt under nextInt did NOT solve the issue.

  • In the switch statement I would have ended each choice with a break and just used the normal loop structure rather than using continue. – NomadMaker May 21 '20 at 04:36
  • I just tested this code (without the switch statement) and I only had to type return once after each choice. Do you have any additional code that might make the difference? What operating system are you using this with? – NomadMaker May 21 '20 at 04:42
  • @VishwaRatna No, I put scan.nextLine() after the int scan and it still is making me enter twice. –  May 21 '20 at 04:42
  • 2
    @Markusovich Just replace this `choice = Integer.parseInt(scan.nextLine())` with your statment because `nextLine()` reads the remainder of the current line even if it is empty. `nextInt()` reads an integer but does not read the escape sequence `\n`. – Deep Dalsania May 21 '20 at 04:43
  • @Markusovich You need to put the entire useful code into the question. Your 'scan.nextLine() is causing you to have to use a second return to give nextLine() something to do. Remove that and things will probably work again. – NomadMaker May 21 '20 at 04:56
  • @NomadMaker Check edit. –  May 21 '20 at 05:17
  • Read the link i provided a 1st comment, that has your solution. – Vishwa Ratna May 21 '20 at 05:22
  • @VishwaRatna That does not work. I tried it. –  May 21 '20 at 05:31
  • x.roll() -- what is x? – NomadMaker May 21 '20 at 16:14
  • You should create an [MRE]. The code you give is incomplete, and we can't compile it without a lot of modification which may hide the problem. – NomadMaker May 21 '20 at 16:15

0 Answers0