Title: StringEqualsComparison
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

String Equals Comparison

Unlike integers and other primitives, the operators <, <=, >, >=, = =, != don't work the way you would expect. This is because Strings variables are really memory addresses to where the actual class lives. The operators work on the address and not the contents. Usually we are interested in comparing the contents and not the actual addresses.

In the video, we demonstrate using the equals() method to do a proper equality comparison with the String class. The example created on the video is as follows:


public class StringCompare {

    public static void main(String[] args) {

        int n1=1, n2 =1;

        if (n1 == n2)
            System.out.println("n1 and n2 are the same");

        String s1="123", s2="";
        s2 += "123";

        System.out.println("s1="+s1);
        System.out.println("s2="+s2);

        if (s1 == s2)
            System.out.println("s1 and s2 are the same");
        else
            System.out.println("s1 and s2 are NOT the same");

        if (s1.equals(s2))
            System.out.println("s1 and s2 have equavalent values");

    }

}


In addition to using the equals() method, other comparisons are demonstrated in the following example from the textbook:



public class StringComparisonDemo
{
    public static void main(String[] args)
    {
        String s1 = "Java isn't just for breakfast.";
        String s2 = "JAVA isn't just for breakfast.";

        if (s1.equals(s2))
            System.out.println("The two lines are equal.");
        else
            System.out.println("The two lines are not equal.");

        if (s2.equals(s1))
            System.out.println("The two lines are equal.");
        else
            System.out.println("The two lines are not equal.");

        if (s1.equalsIgnoreCase(s2))
            System.out.println("But the lines are equal, ignoring case.");
        else
            System.out.println("Lines are not equal, even ignoring case.");

        String s3 = "A cup of java is a joy forever.";
        if (s3.compareToIgnoreCase(s1) < 0)
        {
            System.out.println("\"" + s3 + "\"");
            System.out.println("precedes");
            System.out.println("\"" + s1 + "\"");
            System.out.println("in alphabetic ordering");
        }
        else
            System.out.println("s3 does not precede s1.");
    }
}




Java Practice(JPHG):

1. (5 points) Create a program with a while loop that prompts for names and prints them out. When a name is received that has the value of "quit", the while loop stops. Some sample output from your program might be:

Enter in a name: Fred
Fred was entered
Enter in a name: Sam
Sam was entered
Enter in a name: quit
Program exitting

Hint: remember the equals method described above.
2. (5 points) Create a program that has a while loop that prints out the numbers:
1
2
3
4
5

after doing this, your program prints out a question:
Do you want to print out 5 numbers again (y/n)?
If the user enters a string that starts with the character 'y' then the 5 numbers are printed again. Otherwise, the program exits.

HINT: This will take 2 nested while loops. The inner loop will print the 5 numbers. The outer loop should probably be a "do-while" loop where the prompt occurs and we then collect the user's response. If the response string starts with a 'y' we loop again. Otherwise, the outer loop exits. Remember that you can obtain with first character of a String response with the charAt(0) method. For example:

String response ="yes";
char firstLetter = response.charAt(0);

if (firstLetter == 'y')
...........

Last Modified: December 23, 2014 3:29 PM