Note that there are some explanatory texts on larger screens.

plurals
  1. POFor loop problems
    primarykey
    data
    text
    <p>I'm trying to create a loop where the user enters some values for pizza toppings. They can have pepperoni, pineapple etc. When they type "done" I want it to break out of the loop. I have this all working. Its just my for loop which is the problem. I have 4 pizza topping, so naturally I put it to loop around 4 times. But if the user types in a wrong value, that uses up one of my 4 loops. Now I could set the counter to 200 so they are very unlikely to get it wrong that many times. But i'm hoping there is a cleaner version. So it will keep prompting the user until "done" is typed in. I tried using a while loop, with the do-while nested within it. Something like </p> <pre><code> while( size != "done") </code></pre> <p>But that didn't work either. Hes my code : </p> <pre><code> public List&lt;String&gt; Input(){ List&lt;String&gt; toppings = new ArrayList&lt;String&gt;(); Scanner sc = new Scanner(System.in); String size; System.out.println("What pizza toppings; Pepperoni, Fungi, Pineapple or Bacon, type 'done' when your finished: "); FORLOOP: for(int i = 0; i &lt; 4 ; i++){ do { size = sc.next(); if ( size.equalsIgnoreCase( "Pepperoni") || size.equalsIgnoreCase( "Fungi" ) || size.equalsIgnoreCase( "pineapple" ) || size.equalsIgnoreCase( "bacon" )) { toppings.add(size); } else if( size.equalsIgnoreCase( "done" ) ){ break FORLOOP; } else{ System.out.print("Choose either 'Pepperoni', 'Fungi', 'bacon' or 'pineapple' Toppings, type 'done' when finshed: "); } } while ( false ); } return toppings; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload