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 6 of 6

Thread: how do I use my previous string compare to current string?

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    6
    Thanks
    5
    Thanked 1 Time in 1 Post

    Question how do I use my previous string compare to current string?

    I'm having trouble to compare two string from my LinkedList. I took me 2 days now trying figure out how to compare the current string to previous string in the linkedlist. Please help me out. Thank in advance. Here is my code.

    public int compareTo(LinkedListNode n){
    		//Compare two string
    		String myHead = data.toLowerCase();
    		String comparableHead = data.toLowerCase();
     
    		return (myHead.compareTo(comparableHead));
    	}


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,733
    My Mood
    Mellow
    Thanks
    232
    Thanked 706 Times in 692 Posts

    Default Re: how do I use my previous string compare to current string?

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    The compareTo() method you posted doesn't compare the current object's data to the previous object's. Are you asking how to change the method you posted to do that, or would you prefer writing a new method that does what you've asked?

    Something to consider, a linked list typically has a pointer to the next item in the list, not the previous, so you'd have to create an iterator to find the item in the list previous to the current object and then do the comparison. A doubly linked list would also have a pointer to the previous object, making the solution somewhat easier, but either way is doable.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    sydethsam (March 2nd, 2014)

  4. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    6
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: how do I use my previous string compare to current string?

    Thank for reply. Either change my method or writing a new method is work for me. The thing is I'm not really good at Linkedlist at all. Here is my whole LinkedListNode class. Please let me know if my question make you confuse

    public class LinkedListNode
    {
     
    	private String data;
    	private LinkedListNode next;
     
     
    	public LinkedListNode(String data)
    	{
    		this.data = data;
    		this.next = null;
    	}
     
    	public String getData()
    	{
    		return data;
    	}
     
    	public LinkedListNode getNext()
    	{
    		return next;
    	}
     
    	public void setNext(LinkedListNode n)
    	{
    		next = n;
    	}
     
    	public int compareTo(LinkedListNode n){
    		//Compare two string
    		String myHead = data.toLowerCase();
    		String comparableHead = data.toLowerCase();
     
    		return (myHead.compareTo(comparableHead));
    	}
    }

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,974
    Thanks
    52
    Thanked 2,269 Times in 2,241 Posts

    Default Re: how do I use my previous string compare to current string?

    how to compare the current string to previous string
    As the code goes through the links in the linked list going from one node to the next node, it needs to save the link to the previous node to be able to compare the contents of the current node to the previous node.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    sydethsam (March 2nd, 2014)

  7. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    6
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: how do I use my previous string compare to current string?

    could you provide a small code how to save link to previous node? I'm trying to thinking in my head, but I just have no idea. I try to look to google too, but still no helpful source code. Thank in advance.
    One more thing, how do I avoid to get ban from asking question in here, because I got ban from stackoverflow.com, they said that I do have low reputation.

  8. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,733
    My Mood
    Mellow
    Thanks
    232
    Thanked 706 Times in 692 Posts

    Default Re: how do I use my previous string compare to current string?

    "I'm trying to think, and nothin' happens." - Curly Howard

    For a method:

    compareTo( Node node )

    where 'node' is the current node's previous node:

    1. Find the current node's previous node - this is the hard part
    A. Set Node temp = head
    B. If temp.next == current, temp is previousNode in Step 2
    C. Else temp = temp.next, return to B
    D. If here, there is no previousNode

    2. Call compareTo( previousNode )

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

    sydethsam (March 3rd, 2014)

Similar Threads

  1. [SOLVED] How To Compare String input to char Array....
    By Praetorian in forum Java Theory & Questions
    Replies: 13
    Last Post: February 12th, 2014, 02:34 AM
  2. Replies: 2
    Last Post: September 28th, 2013, 07:36 PM
  3. compare two strings to see if they end with the same sub-string
    By tuathan in forum Loops & Control Statements
    Replies: 2
    Last Post: July 7th, 2012, 06:56 AM
  4. [SOLVED] How to compare a parameter value to a string literal inside EL?
    By Lord Voldemort in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: July 28th, 2011, 06:58 PM
  5. 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

Tags for this Thread