0

I am writing a program for class in which I analyze different product codes entered. It is pretty simple but I am having a problem. I am trying to end a loop if a user enters an "E" or "e". However, it does not end the loop at all. This is at the end of a while statement so setting loop to false should end it and it doesn't even output the totals either so I have messed something up. Code is a string type.

        // Prompt the user for another company code or exit
        System.out.print("Enter the company code or type 'e' to exit: ");

        // Input the user's company code
        code = scan.nextLine();

        // Check to see if the user wants to exit
        if (code == "e" || code == "E") {
            // Output final statistics
            System.out.print("Total valid codes: " + valid + "/n");
            System.out.print("Total banned codes: " + banned);

            // End the loop     
            loop = false;
        }

Any ideas? Thanks!

2
  • I can't see any loop in code? Commented Jul 18, 2011 at 5:36
  • can you put in the loop code? I'm thinking you want to use "break;" instead to end the loop, but I don't know what your loop looks like. Commented Jul 18, 2011 at 5:36

11 Answers 11

6

You need to use code.equals("e") || code.equals("E") (or just code.equalsIgnoreCase("e")). This is because == does identity comparison ("are x and y the same object?"), whereas equals does value comparison ("do x and y have the same value?").

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much to you and everyone who answered.
as a followup question, I take TRV2475A5R-14 and create a character array for just 2475. I need to make sure it contains only numeric characters. How do you recommend I do this?
@Brooks: You should ask a new question on the site for that. However, here's a one-line answer: code.matches("\\d+").
4

When comparing Strings you should always use the equals method rather than ==, so

if ("e".equals(code) || "E".equals(code))

is probably what you want.

The reason for this is that Strings are special objects in Java. They are immutable, and they can be interned to optimize memory usage. Constants (like the "e" and "E" in your code) are automatically interned by the compiler, but the scanLine method will probably return a non-interned String so the == comparison will fail.

Remember, when we're talking about objects, == checks for reference equality not value equality, i.e. a == b means "do a and b refer to the same object?". It's possible for a.equals(b) to be true but a == b to be false.

1 Comment

+1 for talking about interning; I am currently writing some slides and one of the topics I talk about is how immutable objects with equal value can be coalesced and/or interned. :-D
2

Use equalsIgnoreCase()

Your code will be

if (code.equalsIgnoreCase("e")) {
            // Output final statistics
            System.out.print("Total valid codes: " + valid + "/n");
            System.out.print("Total banned codes: " + banned);

            // End the loop     
            loop = false;
        }

Comments

1

Compare strings using .equals() not ==

Comments

1

Strings are compared via equals not ==

if (code.equals("e") || code.equals("E")) {

Comments

1

Use .equals when comparing strings.

In your case you can even use

if (code.equalsIgnoreCase("e")) {

The reason is that == checks whether two objects are the same object. You can have two different string objects represent the same string.

Comments

1

Use .equalsIgnoreCase("e"), == compares the addresses of the Objects in memory and .equals(String) is case sensitive.

Comments

1

Try this:

** LOOP * {

    // Prompt the user for another company code or exit
    System.out.print("Enter the company code or type 'e' to exit: ");

    // Input the user's company code
    code = scan.nextLine();

    // Check to see if the user wants to exit
    if (code.equals("e") || code.equals("E")) {  // <====== see new code
        // Output final statistics
        System.out.print("Total valid codes: " + valid + "/n");
        System.out.print("Total banned codes: " + banned);

        // End the loop     
        //loop = false;
        break;  // <====== see new code
    }

}

Comments

0

I would say for things like codes its better to use Java enums. With enums you can use == operator for comparison.

Comments

0

It's better to use

code.equalsIgnoreCase("e")

Comments

0

Using the explanation given in https://stackoverflow.com/a/513839/1394464

== tests for reference equality.

.equals() tests for value equality.

Therefore, if you actually want to test whether two strings have the same value you should use .equals() instead of ==.

So

if(code == "e" || code == "E")

should become

if(code.equals("e") || code.equals("E"))

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.