Ad

Why Can't I Escape The While Loop?

- 1 answer

I am having trouble with this while loop because it is supposed to ask the user to enter as many artists as they want and to enter done when they have finished but when I enter done it stays asking me for names.

import java.util.*;
public class peer01{
    public static void main(String[] args) {
        Scanner scnr=new Scanner(System.in);
        LinkedList list=new LinkedList();
        System.out.println("Enter your favotite artists first name one at a time");
        while(!scnr.next().equals("done")){
            list.add(scnr.next());
        }

    }
}
Ad

Answer

First, lets start with what is going wrong.

    while(!scnr.next().equals("done")){
        list.add(scnr.next());
    }

Each time you call next(), it reads a word.

Notice that you have twonext() calls for each loop iteration. On each iteration you are doing the following.

  • Read a word and test if it is "done".
  • Read another word and add it to the list.

That means that your code is adding alternate words to the list. And if the user only types "done" on the even alternates, the loop won't stop.


You actually need to implement this logic:

  • Loop
    • Read a word and test if it is "done".
    • If it is "done" terminate the loop
    • If it is not "done" add it to the list and continue the loop.

There are many ways to write that as Java code, but they all involve saving the word that you read in a temporary. So lets start with this:

 String word;
 word = scanner.next();
 while (!word.equals("done")) {
     list.add(word);
     word = scanner.next();
 }

So, we start by reading a word. Then we test it (in the while condition).

  • If it NOT "done" we add it to the list, read the next word and ... repeat the test
  • If it is "done" we end the loop.

The above would be OK to hand in as a solution (IMO). But there is a more concise way to write it:

 String word;
 while (!((word = scanner.next()).equals("done"))) {
     list.add(word);
 }

What is going on here?

The key to understanding this is to notice that we have an assignment expression in the while condition.

 !((word = scanner.next()).equals("done"))

This says:

  1. read a word and assign it to word.
  2. test the value of the assignment expression (i.e. the value assigned to word) to see if it is "done".
  3. perform the logical negation of that ... to give the value for the while to use.

What we have done is combine the read, assign and test into a single expression.

You will probably encounter this idiom in the future, so it is a good idea to understand what it is doing. Whether you should use the idiom in your own code is a matter of personal (or team) opinion. (I personally would use it.)


I should also note that

    LinkedList list = new LinkedList();

is using "raw" types. This is poor code ... and the compiler should warn you about it. The best way to write that would be:

    List<String> list = LinkedList<>();

But that is a different lesson that you will only be able to fully understand when you have learned about generic types, etcetera.

Ad
source: stackoverflow.com
Ad