简体   繁体   中英

How to keep entering a string until user enters done

JAVAHey so I am trying to make an arraylist for some "ingredients" the user input wants or (does not want) So they can pick as many ingredients or no ingredients

The input is in string and after the customer is done choosing ingredients they can type "done"

So what I want is to store as many ingredients as the customer wants in the arraylist and when they type done then it ends the program

Scanner cs = new Scanner(System.in);
 ArrayList<String> toppings = new ArrayList<String>();
            System.out.println("What kind of toppings would you like? Avocado Bacon Cheese Pickles(when done type 'done'");

 while(cs.hasNext()) {
                 toppings.add(cs.next());
                 String input = cs.nextLine();
                 if(input.equalsIgnoreCase("done")) 
                      break;

                 }

Here's how I would do it:

 Scanner cs = new Scanner(System.in);
 ArrayList<String> toppings = new ArrayList<String>();
 System.out.println("What kind of toppings would you like? Avocado Bacon Cheese Pickles(when done type 'done'");

 String input = cs.nextLine();

 while(!input.equals("done")) {
     toppings.add(input);
     input = cs.nextLine();
 }

In my opinion, it's better to select items from a menu and enter just a menu item letter or a menu item number rather than entering in an entire word like Avocado since this is extremely prone to typo's and therefore increasing the need for input validation. A menu selection may look something like this:

What kind of toppings would you like?
1) Avacodo     2) Bacon       
3) Cheese      4) Pickels     
5) Tomato      6) Lettuce     
7) Onions      8) Done        
Select Topping --> |

Now, rather than typing in the desired topping the User merely needs to enter a menu item number. This way all you need to do is validate that a number was indeed supplied and it is in fact within the menu items range (1 to 8 for example).

Allowing the User to add to the selection even though Done was selected doesn't hurt either, for example:

What kind of toppings would you like?
1) Avacodo     2) Bacon       
3) Cheese      4) Pickels     
5) Tomato      6) Lettuce     
7) Onions      8) Done        
Select Topping --> 8

The toppings you selected are:
Pickels        Tomato 

Do you want to (A)dd a topping or (C)ontinue? --> 

Here is some sample code with this put into practice:

Scanner cs = new Scanner(System.in);
String[] variousToppings = {"Avacodo", "Bacon", "Cheese", "Pickels",
                            "Tomato", "Lettuce", "Onions", "Done"};
ArrayList<String> desiredToppings = new ArrayList<>();
int choice = 0;
while (choice == 0) {
    System.out.println();
    System.out.println("What kind of toppings would you like?");
    for (int i = 0; i < variousToppings.length; i++) {
        System.out.printf("%-15s", i + 1 + ") " + variousToppings[i]);
        if (i % 2 != 0 || i == (variousToppings.length - 1)) {
            System.out.println();
        }
    }
    System.out.print("Select Topping --> ");
    String selection = cs.nextLine();
    if (!selection.matches("\\d") || Integer.parseInt(selection) < 1
            || Integer.parseInt(selection) > variousToppings.length) {
        System.err.println("Invalid Entry! Select a topping choice from 1 to "
                + variousToppings.length + "!");
        continue;
    }
    choice = Integer.parseInt(selection);
    if (choice == variousToppings.length) {
        // 'Done' was selected!
        System.out.println();
        System.out.println("The toppings you selected are:");
        for (int j = 0; j < desiredToppings.size(); j++) {
            System.out.printf("%-15s", desiredToppings.get(j));
            if (j % 2 != 0) {
                System.out.println();
            }
        }
        selection = "";
        while (selection.equals("")) {
            System.out.println();
            System.out.print("Do you want to (A)dd a topping or (C)ontinue? --> ");
            selection = cs.nextLine();
            if (!selection.matches("(?i)[ac]")) {
                System.err.println("Invalid Entry (" + selection + ")! Enter either A or C.");
                selection = "";
            }
        }
        if (selection.equalsIgnoreCase("c")) {
            break;
        }
    }
    if (desiredToppings.contains(variousToppings[choice - 1])) {
        System.err.println("You have already selected that topping! "
                         + "(" + variousToppings[choice - 1] + ")");
        choice = 0;
        continue;
    }

    if (!variousToppings[choice - 1].equalsIgnoreCase("done")){
        desiredToppings.add(variousToppings[choice - 1]);
    }
    choice = 0;
}

Here's the fix you need in our code

    Scanner cs = new Scanner( System.in );
    ArrayList<String> toppings = new ArrayList<String>();
    System.out.println( "What kind of toppings would you like? Avocado Bacon Cheese Pickles(when done type 'done'" );

    String nextLine = cs.nextLine();
    if( !nextLine.equalsIgnoreCase( "done" ) )
    {
        toppings.add( nextLine );
        while( true )
        {
            nextLine = cs.nextLine();
            if( nextLine.equalsIgnoreCase( "done" ) )
            {
                break;
            }
            toppings.add( nextLine );
        }
    }
    System.out.println( toppings );
Scanner scanner = new Scanner(System.in);
List<String> toppings = new ArrayList<String>();
String resource = null;
while(true) {
    System.out.println("Enter Resource : ");
  resource = scanner.nextLine();
  if(resource.equalsIgnoreCase("done")) {
      break;
 }else {
   toppings.add(resource);
}
}
System.out.println(toppings);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM