Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Compare method

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    1
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Compare method

    Hello, I am writing a code that its showing the process of renewing your mail address when you move.

    this is part of the program that i need help on

    public class NewDataEntry 
    	{
    		private String name;
    		private String oldAddress;
    		private String newAddress;
     
     
     
    		// a method that compares the old and the new address. if the address is the same the program     goes through it
    		// if they are not the same it's false, and it needs the new address.
     
     
    		// a subclass for a new entry if the addresses are not the same.
     
    		public String getName()
    		{
    			return name;
    		}
    		public String getoldAddress()
    		{
     
    		}
    		public String getnewAddress()
    		{
     
    		}
     
    	}

    i need a method that it will compare the oldAddress with the newAddress, when it is inputed.
    if the new address that is inputed its the same thing as we have on file than the program skips that method but if its not, it has to compare and replace it with the new one....Im having hard time getting this ...can someone guide me?
    Last edited by copeg; October 12th, 2010 at 09:39 PM. Reason: Code tags


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Compare method

    See String (Java Platform SE 6), most notably the equals method.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Compare method

    Ok, this is a problem that I see ALL THE TIME with beginners to JAVA, so lets get this straight before it becomes an issue later down the line.

    In JAVA, simple type numbers are compared for equality by using the double-equals (==) operation. Simple type numbers are ints, doubles, longs, floats, ect.

    Objects are different however, and since String is essentially an Object, it follows the same comparing rules as all other Objects. Objects can be compared in two different ways, but each way does drastically different things. For Objects, if you are trying to check the values of the those Objects (like if two Strings are equal to each other) for equality, you cannot use the double-equals (==) operation. Instead, you have to use the XObject.equals(XObject) method, where XObject is a Comparable Object. Don't worry about Comparable Objects right now, just know that String is a Comparable Object. The second way to compare Objects is with the double-equals (==), however this doesn't check the value of the object. Instead, it checks the Object's value in memory. This is only necessary if you want to test if an Object has two or more variable names for whatever reason. From what I've found, fairly useless.

    Now, String is unique in that it provides us with an additional way to compare. As well as the String.equals(String) method, there is also the String.equalsIgnoreCase(String) method. The difference is exactly what the method says. The first checks for equality, but is case sensitive. The second checks for equality, but ignores the case while doing so. Ignoring the case is useful if you want to compare two Strings together, but are unsure if they have the same case, but want them to equal each other regardless of what is capitalized and what isn't.

    I usually ignore case when comparing Strings, simply because I take in a lot of user input and, as a programmer, you have to assume the user is an idiot and will incorrectly use your program or incorrectly input. As a result, you have to compensate for your user's stupidity ("ignorance", to be nice) and put in as many barriers and quick-fixes as possible to prevent the dreaded program crashes.

    However, for your assignment, I'm not sure if you should be checking case or not while comparing Strings. That would be something to ask.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  4. #4
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Compare method

    Sorry Aussie, but I feel a need to nitpick here to set the record straight.

    Quote Originally Posted by aussiemcgr View Post
    In JAVA, simple type numbers are compared for equality by using the double-equals (==) operation. Simple type numbers are ints, doubles, longs, floats, ect.
    They're called primitives. That includes the primitive numeric types and boolean.

    Objects are different however, and since String is essentially an Object, it follows the same comparing rules as all other Objects. Objects can be compared in two different ways, but each way does drastically different things. For Objects, if you are trying to check the values of the those Objects (like if two Strings are equal to each other) for equality, you cannot use the double-equals (==) operation. Instead, you have to use the XObject.equals(XObject) method, where XObject is a Comparable Object.
    Overriding equals(...) to return true for equivalent object states is orthogonal to implementing Comparable, granted that a class that implements Comparable must always override equals(...) for consistency, unless the implementation of Comparable has to fulfill some extremely rare (aka wierd ) requirement.

    The reverse, as implied in your statement, is not true. Comparing String or any other objects for equality of state using equals(...) does not in any way depend on the class implementing Comparable.

    Don't worry about Comparable Objects right now, just know that String is a Comparable Object. The second way to compare Objects is with the double-equals (==), however this doesn't check the value of the object. Instead, it checks the Object's value in memory.
    More strictly, it checks the equality of the references. Two references (aka reference variables) are equal if both refer to the same object. (Two primitive variables are equal if they contain the same value, but everyone knows that )

    This is only necessary if you want to test if an Object has two or more variable names for whatever reason. From what I've found, fairly useless.
    Very useful especially in event driven programming where the event holds a reference to the object that created/dispatched it. For example, in a listener that's added to several GUI components to perform the same action, but only on the component that triggered the event.

    Now, String is unique in that it provides us with an additional way to compare. As well as the String.equals(String) method, there is also the String.equalsIgnoreCase(String) method. The difference is exactly what the method says. The first checks for equality, but is case sensitive. The second checks for equality, but ignores the case while doing so. Ignoring the case is useful if you want to compare two Strings together, but are unsure if they have the same case, but want them to equal each other regardless of what is capitalized and what isn't.

    I usually ignore case when comparing Strings, simply because I take in a lot of user input and, as a programmer, you have to assume the user is an idiot and will incorrectly use your program or incorrectly input. As a result, you have to compensate for your user's stupidity ("ignorance", to be nice) and put in as many barriers and quick-fixes as possible to prevent the dreaded program crashes.

    However, for your assignment, I'm not sure if you should be checking case or not while comparing Strings. That would be something to ask.
    Relevant, and well explained.

    db

  5. #5

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Compare method

    Quote Originally Posted by Darryl.Burke View Post
    Sorry Aussie, but I feel a need to nitpick here to set the record straight.


    1) They're called primitives. That includes the primitive numeric types and boolean.


    2) Overriding equals(...) to return true for equivalent object states is orthogonal to implementing Comparable, granted that a class that implements Comparable must always override equals(...) for consistency, unless the implementation of Comparable has to fulfill some extremely rare (aka wierd ) requirement.

    The reverse, as implied in your statement, is not true. Comparing String or any other objects for equality of state using equals(...) does not in any way depend on the class implementing Comparable.


    3) More strictly, it checks the equality of the references. Two references (aka reference variables) are equal if both refer to the same object. (Two primitive variables are equal if they contain the same value, but everyone knows that )


    4) Very useful especially in event driven programming where the event holds a reference to the object that created/dispatched it. For example, in a listener that's added to several GUI components to perform the same action, but only on the component that triggered the event.


    Relevant, and well explained.

    db
    Wow, pwnd... To be fair, I wrote that at like 11pm.

    Either way,
    1) Primitive! I was looking for that word but couldn't remember it.
    2) I knew classes that don't implement Comparable can be compared, but I was unsure the details so I decided not to go in depth with it since it was irrelevant for Evelina for the time being.
    3) I've got nothing for this one. Learning a little more every day.
    4) I can't believe I didn't notice that. I compare GUI elements ALL THE TIME in my Listeners.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  7. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Compare method

    Quote Originally Posted by Evelina View Post
    Hello, I am writing a code that its showing the process of renewing your mail address when you move.

    this is part of the program that i need help on

    public class NewDataEntry 
    	{
    		private String name;
    		private String oldAddress;
    		private String newAddress;
     
     
    // where are the values of name, oldAddress, and newAddress initialized?  if nowhere, then you'll be comparing null String anyway and will likely get a Null Pointer Exception
     
    		// a method that compares the old and the new address. if the address is the same the program     goes through it
    		// if they are not the same it's false, and it needs the new address.
     
     
    		// a subclass for a new entry if the addresses are not the same.
     
    		public String getName()
    		{
    			return name;
    		}
    		public String getoldAddress()
    		{
    			return oldAddress;
    		}
    		public String getnewAddress()
    		{
    			return newAddress;
    		}
     
    public boolean isTheSame()
    {
    if (getOldAddress().equals(getNewAddress())
    {
    return true;
    }
     
    else
    return false;
    }
     
    if (!isTheSame())
    {
    Subclass sub = new Subclass();
    // whatever you wanted to do with the subclass.  It won't do anything unless call its methods.  
    }
     
    else
    {
    // whatever else you wanted to do
    }
    	}

    i need a method that it will compare the oldAddress with the newAddress, when it is inputed.
    if the new address that is inputed its the same thing as we have on file than the program skips that method but if its not, it has to compare and replace it with the new one....Im having hard time getting this ...can someone guide me?
    Compare to in what way? String implements an interface called Comparable that has a method called compareTo(). Not sure if that's what you were looking for.

    Java 2 Platform SE v1.3.1: Class String

    However, if you're looking for exact equality, use .equals.

    I showed how to do it above by editing your code.
    Last edited by javapenguin; October 15th, 2010 at 10:25 PM.

  8. #8
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Compare method

    Quote Originally Posted by javapenguin View Post
    Please, please don't post links to APIs for ancient versions of Java. That can cause a lot of confusion about 'missing' methods that were added in later versions. For example, String has 2 constructors and 2 methods that were added in version 1.6, apart from a host of stuff that was added in 1.5 and 1.4.
    String (Java Platform SE 6)

    db

  9. The Following User Says Thank You to Darryl.Burke For This Useful Post:

    javapenguin (October 16th, 2010)

  10. #9
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Compare method

    Every time I search, it seems to come to the old version of Java, that's what the first entry on the search engine results page shows me.

    I think. How often do they update?

    Have they created a handy file that will use a JFileChooser and help save and load it for you so you don't have to fiddle with that nightmare by yourself?

    Also, by any chance did they define the String class by having a String be

    private Vector <Character> cVector;
    public String(cVector)
    {

    }

    That would also make a getCharAt(int i) a whole lot easier if it was defined using characters. Either an array or a Vector.
    Last edited by javapenguin; October 16th, 2010 at 01:25 PM.

  11. #10
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Compare method

    Quote Originally Posted by javapenguin View Post
    Every time I search, it seems to come to the old version of Java, that's what the first entry on the search engine results page shows me.
    That's because search engines rank pages (among other factors) by the number of times they have been viewed / clicked through, and the older API has been around longer. People who blindly go for the first result heko keep the old stuff at or near the top of the listings.
    I think. How often do they update?
    No idea. From the time I learned Java, it's been 1.6 aka Java SE 6. 1.7 was expected this year, but the Oracle takeover may affect that.
    Have they created a handy file that will use a JFileChooser and help save and load it for you so you don't have to fiddle with that nightmare by yourself?
    That's not the purpose of a JFileChooser. A class should have one responsibility, and fulfill it well.
    Also, by any chance did they define the String class by having a String be

    private Vector <Character> cVector;
    public String(cVector)
    {

    }

    That would also make a getCharAt(int i) a whole lot easier if it was defined using characters. Either an array or a Vector.
    I don't have any problems with the API as it stands, and I would guess (maybe wrongly) that you're carrying over some baggage from C. Languages have their differences; if they didn't there would be only one language.

    db

Similar Threads

  1. Trying to somehow Compare Generics
    By Omega_ryan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2010, 12:58 PM
  2. how to compare two set values
    By humdinger in forum Collections and Generics
    Replies: 1
    Last Post: March 13th, 2010, 11:46 AM
  3. String + Compare // Might be too easy for ya
    By Jangan in forum Java Theory & Questions
    Replies: 1
    Last Post: October 18th, 2009, 05:40 PM