2
do
{
    System.out.println("Type another number to continue or type 'End' to end");
    end = scan.next();

    if("end".equals(end)==false||"End".equals(end)==false)
    {
        num2 = Integer.parseInt(end);   


        int j = 0;          
        while (j < num2)
        {
            System.out.println(name);
            j++;
        }

        if(j == 0)
        {
            num2 = 0;   
        }
    }   
} while("end".equals(end)==false||"End".equals(end)==false);    

The String comparison keeps failing. When you type 'End' or 'end' it tries to parse it into an integer and returns an error. Idk why this is happening.

2
  • I'm not sure if the if is necessary, I put that in because I thought it was trying to parse into an Int before it ended the loop and that was causing the error. However, I still get the error with the conditional. Commented Sep 26, 2013 at 20:16
  • Maybe cause you are actually parsing it ? num2 = Integer.parseInt(end); Commented Sep 26, 2013 at 20:16

3 Answers 3

3
if("end".equals(end)==false||"End".equals(end)==false)

Think about it: that would always be true. If the user typed end, then the first case would be true and the second false.

You could fix it by using && instead, but a better method would be:

if(!("end".equalsIgnoreCase(end)))

Also, so you don't have to check the condition twice, you could just use

while (true) {
    System.out.println("Type another number to continue or type 'End' to end");
    end = scan.next();
    if ("end".equalsIgnoreCase(end)) break;
    // now do your stuff
    num2 = Integer.parseInt(end);
    // ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

Man, the problem is always so obvious once you see it. Thanks errybody.
@ChristianBaker You're welcome! If this answer solved the problem, please click on the green check to the left so that the question is marked as resolved.
2

This line will always be true:

if("end".equals(end)==false||"End".equals(end)==false)

because at least one of them will always be true. You want it not to equal "end" and not to equal "End". Replace || with &&:

if("end".equals(end)==false && "End".equals(end)==false)

There's no need to compare the value with false; just use the ! negation operator:

if(!("end".equals(end)) && !("End".equals(end)))

You will need to make a similar change to the condition at the end of your do-while loop.

3 Comments

I think a cleaner way for that line might be if (!("end".equals(end) || "End".equals(end))), but personally I like passing the string to check into the method: if (!(end.equals("end") || end.equals("End"))). It's all preference really.
@Rogue They are logically equivalent: not (condA || condB) and not condA && not condB. Also, most prefer using "literal".equals(variable)" to avoid a possible NullPointerException if variable is null.
Hm, I can see that perspective. Could potentially be easier since you wouldn't necessarily need to null check.
0

You are parsing end to int num2 by the statement num2 = Integer.parseInt(end);.
That will you NumberFormatException

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.