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!

Bhushan
  • 18,329
  • 31
  • 104
  • 137

11 Answers11

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

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
  • Thank you so much to you and everyone who answered. – Brooks Fitch Jul 18 '11 at 05:38
  • 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 Fitch Jul 18 '11 at 05:42
  • @Brooks: You should ask a new question on the site for that. However, here's a one-line answer: `code.matches("\\d+")`. – C. K. Young Jul 18 '11 at 05:44
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.

Cameron Skinner
  • 51,692
  • 2
  • 65
  • 86
  • +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 – C. K. Young Jul 18 '11 at 05:42
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;
        }
1

Compare strings using .equals() not ==

Alex Gitelman
  • 24,429
  • 7
  • 52
  • 49
1

Strings are compared via equals not ==

if (code.equals("e") || code.equals("E")) {
fyr
  • 20,227
  • 7
  • 37
  • 53
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.

trutheality
  • 23,114
  • 6
  • 54
  • 68
1

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

Jeffrey
  • 44,417
  • 8
  • 90
  • 141
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
    }

}

JustBeingHelpful
  • 18,332
  • 38
  • 160
  • 245
0

It's better to use

code.equalsIgnoreCase("e")
Anuja Shiran
  • 179
  • 1
  • 4
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"))
Community
  • 1
  • 1
Mohit
  • 891
  • 10
  • 25
0

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

Shekhar
  • 5,771
  • 10
  • 42
  • 48