Home > Net >  How can I stop my code from duplicating menu printing?
How can I stop my code from duplicating menu printing?

Time:09-21

I'm working on an assignment for school and everything is working as expected, but my menu isn't working how I want it to. This is a test class that I made where it's essentially the same as my Main class but many of the cases are just empty so I could try to figure out what was going on with it.

import java.util.Scanner;
public class Test {
    
    public static void main(String[] args) {
        
        Scanner input = new Scanner(System.in);
        Order array[] = new Order[100];
        boolean on = true;
        
        do {
            System.out.println("*** Choice Action "
                      "\nA) Add an Order "
                      "\nB) Compute Total Costs "
                      "\nC) Search for an Order "
                      "\nD) List Orders "
                      "\nE) Quit");
            System.out.println("What action would you like to perform?");
            
            String userInput = input.nextLine().toLowerCase();
            
            switch(userInput) {
            case "add an order": 
                continue;
            case "compute total costs":
                continue;
            case "search for an order":
                System.out.println("Please enter a product name to search: ");
                String search = input.next();
                boolean found = false;
                for (int i = 0; i < 100; i  ) {
                    if (array[i] != null) {
                        if (array[i].productName.equals(search)) {
                            System.out.println("Product found!");
                            break;
                        }
                    }
                }
                if (!found) {
                    System.out.println("Product not found.");
                }
                break;
            case "list orders":
                continue;
            case "quit":
                on = false;
                break;
            default:
                System.out.println("Unknown action");   
            }
            
        } while (on);
        
        input.close();
    }
}

So when I run the code, I end up with this where it displays the menu again twice, the first time just giving the default case and then the second time accepting input. I tried moving the scanner object creation and closing into the do-loop but it just ends up failing after going into the second loop because the scanner can't find the next line.

*** Choice Action 
A) Add an Order 
B) Compute Total Costs 
C) Search for an Order 
D) List Orders 
E) Quit
What action would you like to perform?
search for an order
Please enter a product name to search: 
test search
Product not found.
*** Choice Action 
A) Add an Order 
B) Compute Total Costs 
C) Search for an Order 
D) List Orders 
E) Quit
What action would you like to perform?
Unknown action
*** Choice Action 
A) Add an Order 
B) Compute Total Costs 
C) Search for an Order 
D) List Orders 
E) Quit
What action would you like to perform?

CodePudding user response:

Get a look at line String search = input.next();. I think it should be String search = input.nextLine();.

It should be obvious from your output Unknown action. So, wrong (empty) line read, it is interpreted as Unknown action.

  • Related