2

I'm new to programming and to this website, so here goes. I wanted to write a program that would allow as many input strings as possible to be added to an ArrayList. So I used a while loop in the following code. What I intended was for the loop to break if the input was 0.

import java.util.*;

public class AddToList2
{
static Scanner q = new Scanner(System.in);

public static void main(String[] args)
{
    ArrayList<String> inputlist = new ArrayList<String>();
    while (true)
    {
        System.out.print("Enter something here: ");
        String x = q.nextLine();
        inputlist.add(x);
        if (x.equals("0"));
            break;
    }
}

The program was compiled without error, but sadly, when I ran the program many times, the loop broke no matter what the input was. Any way to solve this?

Well, that was careless of me! Anyway, I had created that program in order to find out what was wrong with this:

ArrayList<String> endangeredlist = new ArrayList<String>();
    ArrayList<Integer> popn = new ArrayList<Integer>();
    while (true)
    {
        System.out.print("Name an animal: ");
        String animal = q.nextLine();
        endangeredlist.add(animal);
        if (animal.equals("EXTERMINATE"))
            break;          
        q.next();
        System.out.print("How many are left in the wild? ");
        int numberleft = q.nextInt();
        popn.add(new Integer(numberleft));
    }

(This is part of a much larger program.) My intention was for the loop to break when the animal name input was EXTERMINATE. Sadly the program throws a NoSuchElement exception if the input first time round was EXTERMINATE, and if I had inputted something else first the loop would start, but then inputting EXTERMINATE second time round does not break the loop. Why is that?

3
  • 1
    Instead of using break, why not simply do: while(!x.equals("0"))? Commented Oct 20, 2012 at 21:13
  • It would cause a compile error. Trust me, I've tried it. It's because x was initialized inside the loop. Commented Oct 20, 2012 at 21:39
  • In this case, you can use a do/while loop, like: do { String x = q.nextLine(); } while (!x.equals("0")); The body of the loop can be kept the same; shortened here for readability. Commented Oct 20, 2012 at 21:50

3 Answers 3

10

You have an extraneous semicolon after your if, which effectively makes it

if (x.equals("0")) { }
break;
Sign up to request clarification or add additional context in comments.

2 Comments

You have the break statement out of the if body.
That's precisely the point. His code ends up being equivalent to what is posted above, which is why it breaks regardless of the condition.
1

You have a semi-colon at the end of your condition.

This turns the break into a statement of its own, without the condition.

Comments

1

Your if statement is broken

if (x.equals("0"));

This is basically saying if (x.equals("0")) do nothing...

This is one of the reasons why you should use parenthesis around your if statements

if (x.equals("0")) {
    break;
}

1 Comment

- This is why you should use parenthesis around your if statements. No it isn't. If he'd written if (x.equals("0")) ; { break; } he would have had exactly the same error. We use { ... } to prevent later inclusion of a second statement intended to be guarded by the if creating an error

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.