Results 1 to 9 of 9
  1. #1
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    148
    Rep Power
    0

    Question Alphabetical Sorting

    I've written a program which reads from the text file "hej.txt", which contains names sorted in alphabetical order. The user enters a name, and then the program searchs for that name in "hej.txt". (The search algorithm I use is the binary search algorithm.) If it is found or not, the program informs you of that. However, whenever I enter a name the program always outputs "The person was not found in hej.txt!" even when I'm completely sure that the file contains the name I entered. Will you please tell me why? (I have a hunch that it has something to do with comparing strings at line 20 and 22.)

    Thanks in advance!

    Java Code:
    import java.util.Scanner;
    import java.io.*;
    
    public class hej{
    	public static void main(String[] args) throws FileNotFoundException{
    		Scanner fileReader = new Scanner(new File("hej.txt")), userInput = new Scanner(System.in);
    		// "hej.txt" contains a bunch of names sorted in alphabetical order so that I can use the "binary search" algorithm.
    	    String elever[] = new String[1000];
    	    int index = 0;
    		while(fileReader.hasNext())
    			elever[index++] = fileReader.nextLine();
    		
    		System.out.print("Enter the name of the person you want to search for: ");
    		String searchFor = userInput.nextLine();
    		
    		// This below is the "binary search" algorithm.
    		int start=0, end=index, m=0;  // The "start" and "end" variables indicate the start and the end of the interval.
    		while(start<=end){
    			m = (start+end)/2;
    			if(searchFor.compareToIgnoreCase(elever[m]) < 0) 
    				end=m-1;
    			else if(searchFor.compareToIgnoreCase(elever[m]) > 0) 
    				start=m+1;
    			else break;
    		}
    		
    		if(searchFor.equalsIgnoreCase(elever[m]))
    			System.out.print("The person was found in \"hej.txt\"!");
    		else
    			System.out.print("The person was not found in \"hej.txt\"!");
    	}
    }

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,416
    Rep Power
    5

    Default Re: Alphabetical Sorting

    Check your end interval. See how many values you really have.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    148
    Rep Power
    0

    Default Re: Alphabetical Sorting

    Are you trying to say that I also searched through the values that were not initialized?

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,416
    Rep Power
    5

    Default Re: Alphabetical Sorting

    No. I am saying look and see what the value of index is after you finish reading in the list.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    148
    Rep Power
    0

    Default Re: Alphabetical Sorting

    The value of "index" is 3, and I have 3 names in the file so it is correct.

  6. #6
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    148
    Rep Power
    0

    Default Re: Alphabetical Sorting

    After a little bit of testing I have noticed that the program, strangely enough, actually works when searching for all names except for the first one.

  7. #7
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,416
    Rep Power
    5

    Default Re: Alphabetical Sorting

    The problem that I have found is that if you try to find a value greater than the last value in the list, you get an exception because you get
    a NPE. This is because the value of m is indexing beyond the available characters. I suggest you put print statements in the code to print out values of m, start, and end after they have changed but before they are used.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    148
    Rep Power
    0

    Default Re: Alphabetical Sorting

    It actually worked all the time, I just forgot that the names had to be sorted... Thanks for your help! :)
    Last edited by Zelaine; 10-30-2013 at 10:19 AM.

  9. #9
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,416
    Rep Power
    5

    Default Re: Alphabetical Sorting

    Well, if I put try to find a value that is alphabetically greater than the last element, I get an NPE.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 2
    Last Post: 10-05-2013, 05:40 AM
  2. array to sort names in alphabetical order
    By leoshiner in forum New To Java
    Replies: 6
    Last Post: 05-01-2011, 12:28 PM
  3. C# Sorting
    By larry_d1990 in forum New To Java
    Replies: 8
    Last Post: 01-27-2011, 09:51 AM
  4. How to sort array objects into alphabetical order...
    By lisalala in forum New To Java
    Replies: 5
    Last Post: 03-03-2009, 11:01 AM
  5. Replies: 20
    Last Post: 05-14-2008, 09:42 AM

Posting Permissions

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