0
   public static void main(String[] args) {

      ArrayList<String> names = new ArrayList<String>();

      names.add(JOptionPane.showInputDialog("Enter type of pie"));

   }

How would I loop this statement? I've tried dowhile (!names.equalsIgnoreCase("q")); but it can't find it.

      names.add(JOptionPane.showInputDialog("Enter type of pie"));
7
  • What do you mean it can't find it? Commented Jul 11, 2015 at 16:16
  • Compile error says can't find symbol "names" Commented Jul 11, 2015 at 16:17
  • Does it work before you add the for loop? I.e does the code you posted above compile? Commented Jul 11, 2015 at 16:18
  • names is a List, obviously it cannot equal "q"; regardless of case. What exactly are you trying to do?? Commented Jul 11, 2015 at 16:18
  • I'm trying to repeat the input dialog until the user enters q Commented Jul 11, 2015 at 16:20

3 Answers 3

1

This work fine.

     String str = null;
     List<String> names = new ArrayList<String>();
     do
     {
      str = JOptionPane.showInputDialog("Enter type of pie");
   if(!str.equalsIgnoreCase("q"))
      names.add(str);
     }while(!str.equalsIgnoreCase("q"));
Sign up to request clarification or add additional context in comments.

2 Comments

So adding "q" to the list is okay?
You can just make it into a while loop and avoid adding q to the List that way - this is rather ugly.
1

Try this...

public static void main(String[] args) throws Exception {
    List<String> names = new ArrayList();
    String input = "";
    while (!input.equalsIgnoreCase("q")) {
        input = JOptionPane.showInputDialog("Enter type of pie");
        if (!input.equalsIgnoreCase("q")) {
            names.add(input);
        }
    }

    System.out.println(names);
}

Comments

0

In your loop, you should iterate through the names list and call equalsIgnoreCase("q") on each element, adding another input dialog each iteration.

consider:

while (names.isEmpty() || !names.get(names.size() - 1).equalsIgnoreCase("q"))
{
    names.add(JOptionPane.showInputDialog("Enter type of pie"));
}

And if you'd like to disregard the name of the pie "q":

while (names.isEmpty() || !names.get(names.size() - 1).equalsIgnoreCase("q"))
{
    names.add(JOptionPane.showInputDialog("Enter type of pie"));
}
// populates the ArrayList names with the JOptionPane user input
if (!names.isEmpty())
{
    names.remove(names.size() - 1);
    // remove the last name inputted by the user
    // since the only way to terminate the loop is by entering "q",
    // you are removing the name of "q" from the list.
}

EDIT: this is a better implementation since it only adds the names which aren't "q":

String userInput = new String();
while (!userInput.equalsIgnoreCase("q"))
{
    userInput = JOptionPane.showInputDialog("Enter type of pie");
    if (!userInput.equalsIgnoreCase("q"))
    {
        names.add(userInput);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.