String methods: CompareTo and Equals


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: String methods: CompareTo and Equals

  1. #1
    Join Date
    Sep 2003
    Posts
    21

    String methods: CompareTo and Equals

    Maybe I misunderstand what is meant by the
    CompareTo and Equals methods of String.

    Here's my code, if I want to use an if statement, how would I do so using these methods?
    I keep getting syntax errors, and believe that I may not be using these methods correctly.

    If using an IF statment is not best way to demonstrate these two methods, please explain why and
    what I should do to you use these methods and get the inferred results.

    Just so you know, I am just asking user for a sentence [input string] and then using different string methods to print different output, based on what each method returns.



    Code:
    import java.util.*;
    import javax.swing.*;
    
    public class stringEx2 {
        
        public static void main(String [] args)
        {
            String input = JOptionPane.showInputDialog 
                            ("Input a sentence or phrase and we'll give some details about it");
           
            System.out.println("Your sentence : " +input);
            
            //length():gives length of string input
            System.out.println("The number of characters in your sentence is " +input.length());
            
            //charAt: gives position of character in string
            System.out.println("The 5th character in your sentence is " +input.charAt(4));
            
            //compareTo: gives comparison of strings
           if (input.compareTo("Hello") > 0)
               System.out.println("You typed the lucky word " +input "/.  So Hello to you!!");
            else
               System.out.println("You did not type the lucky word : Hello");
          
            
            //equals : decides if string has same characters as string passed
            if (input.equals("Hello World"))
               System.out.println("You typed the lucky phrase " +input "/.  So Hello to you!!");
            else
                System.out.println("You did not type the lucky phrase : Hello World");
          
            
            
            
            
           System.exit(0);
           
            
        }
        
        
    }
    Sue

  2. #2
    Join Date
    Mar 2003
    Posts
    834
    "Maybe I misunderstand what is meant by the
    CompareTo and Equals methods of String. "

    Well what do you understand at the moment?

    "Here's my code, if I want to use an if statement, how would I do so using these methods? I keep getting syntax errors, and believe that I may not be using these methods correctly."

    It would help if you posted what these error messages are.

    "If using an IF statment is not best way to demonstrate these two methods, please explain why and
    what I should do to you use these methods and get the inferred results. "

    'if' statements are fine for explaining these.

    Just so you know, I am just asking user for a sentence [input string] and then using different string methods to print different output, based on what each method returns.

    OK, but the lines:
    Code:
            //compareTo: gives comparison of strings
           if (input.compareTo("Hello") > 0)
               System.out.println("You typed the lucky word " +input "/.  So Hello to you!!");
            else
               System.out.println("You did not type the lucky word : Hello");
    Does not make a huge amount of sense. The method compareTo returns an 'int':

    0 (the two strings are equal)
    > 0 The argument is "greater than" the object.
    < 0 The argument is "less than" the object.

    I suggest you read Sun's documentation:

    http://java.sun.com/j2se/1.3/docs/ap...html#compareTo(java.lang.String)
    ArchAngel.
    O:-)

  3. #3
    Join Date
    Sep 2003
    Posts
    21

    CompareTo/Equals

    I guess what I didn't understand until I read abit of the documentation at Sun's site [thank you], is that
    compareTo returns just an int.

    I was thinking if user input the phrase: Hello
    and that equals the Hello in my If statement, then the user would get a message that said they typed the lucky word, Hello, and then say Hello back to the user.

    Code:
    //compareTo: gives comparison of strings
           if (input.compareTo("Hello") > 0)
               System.out.println("You typed the lucky word " +input "/.  So Hello to you!!");
            else
               System.out.println("You did not type the lucky word : Hello");
    And also with the equals method, I was trying to have code compare what phrase user typed in and if it matched my phrase in the the If statement then user gets one kind of message, if not it gets another.

    Since I am not sure of what you would use the
    compareTo and equals methods in a real world scenario, based on what a user might input to an Input Dialog box, do you have any suggestions that I could try out for my class?

    Most of the other methods are similar to things I have learned in C and VB, but these two have me perplexed still.
    Sue

  4. #4
    Join Date
    Mar 2003
    Posts
    834
    It's quite simple:

    .equals()
    Use this when you want to compare whether or not two strings are identical.

    .compareTo()
    Use this when you want to know whether one string is equal to, "greater than" or "less than" another string.

    You'll mostly use .equals().
    ArchAngel.
    O:-)

  5. #5
    Join Date
    Sep 2003
    Posts
    21
    Yes, I would agree.

    My instructor gave another way of understanding the use for CompareTo. If you had an application that was sorting say in alphabetical order, CompareTo would be the method of choice. However, are there other methods of String or other methods of other classes that could be used to "compare and sort"? Is there a class that has methods for "sorting" integers or doubles [ie numbers not alpha characters]?

    Now I get it, thanks.
    Sue

  6. #6
    Join Date
    Mar 2003
    Posts
    834
    Lots.

    Look at classes/interfaces: 'Arrays', 'Comparable' and 'Comparator'.
    ArchAngel.
    O:-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center