Brooks Fitch
Brooks Fitch

Reputation: 1

String issue - Java

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!

Upvotes: 0

Views: 230

Answers (11)

Mohit
Mohit

Reputation: 921

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"))

Upvotes: 0

Anuja Shiran
Anuja Shiran

Reputation: 179

It's better to use

code.equalsIgnoreCase("e")

Upvotes: 0

Shekhar
Shekhar

Reputation: 5931

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

Upvotes: 0

user831722
user831722

Reputation:

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;
        }

Upvotes: 2

Cameron Skinner
Cameron Skinner

Reputation: 54516

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.

Upvotes: 4

JustBeingHelpful
JustBeingHelpful

Reputation: 18990

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
    }

}

Upvotes: 1

Jeffrey
Jeffrey

Reputation: 44808

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

Upvotes: 1

trutheality
trutheality

Reputation: 23455

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.

Upvotes: 1

fyr
fyr

Reputation: 20869

Strings are compared via equals not ==

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

Upvotes: 1

Alex Gitelman
Alex Gitelman

Reputation: 24732

Compare strings using .equals() not ==

Upvotes: 1

C. K. Young
C. K. Young

Reputation: 223193

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?").

Upvotes: 6

Related Questions