开发者

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 eve开发者_运维百科n 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!


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


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.


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


Compare strings using .equals() not ==


Strings are compared via equals not ==

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


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.


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


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
    }

}


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


It's better to use

code.equalsIgnoreCase("e")


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜