Results 1 to 2 of 2
  1. #1
    falkon114 is offline Member
    Join Date
    Dec 2010
    Rep Power

    Default compareTo() problems...

    I'm trying to find the word alphabetically following a user inputted word in a text document... Why is this finding the word at the end of the alphabet contained in the document rather than right after the user word? (i.e. "zulu")
    Java Code:
    String next = null; //variable to hold the value we're looking for.
    word =; //hold the scanners next object
    if(next != null && next.compareTo(word) < 0 && word.compareTo(next) > 0 || next == null && word.compareTo(userTarget) < 0)
    						next = word;
    In my file, it keeps listing the word "zulu", which i assume would be the last word of the alphabet in this particular document. I've tried switching and flipping signs and variables, but this gets me closest to what i need..

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    New Zealand
    Rep Power


    You'll need some sort of loop construct (like "while") to work through the words in the text document checking each against both the userTarget (to see if its after the target word and hence a possible candidate) and against the best candidate you have found so far (to see if its before that word in which case you have a better candidate).

Similar Threads

  1. using the compareTo method
    By anonymous445 in forum New To Java
    Replies: 3
    Last Post: 01-19-2011, 04:53 AM
  2. Sort with CompareTo
    By ScienceLife in forum New To Java
    Replies: 7
    Last Post: 12-01-2010, 02:08 PM
  3. need help wit this compareTo method
    By ShinTec in forum New To Java
    Replies: 4
    Last Post: 02-02-2010, 03:24 AM
  4. Help with interfaces, compareTo etc
    By jigglywiggly in forum New To Java
    Replies: 10
    Last Post: 09-07-2009, 02:52 AM
  5. compareTo()
    By Tsiliadoros in forum Advanced Java
    Replies: 5
    Last Post: 10-03-2008, 02:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts