5 Replies - 13181 Views - Last Post: 19 May 2008 - 09:24 PM Rate Topic: -----

#1 Runesmith  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 07-March 08

Sort/Search Methods

Posted 18 May 2008 - 08:08 PM

Hi, everyone! I have to make a program that allows the user to search and sort through an employee database (the data.txt file mentioned in the first method). Writing out the search method was pretty easy, and I think it'll work, but I'm having a few issues with the sort code. I've tried the bubble sort technique, but I couldn't find a way to implement it probably. Is there a way to get bubble sort to work in this program, or would another type of sort be more appropriate?

Here is my current code:

package readfilebreakuptoarrays;
import java.io.*;
import java.lang.String;
import javax.swing.JOptionPane;
public class Main {  
	public static void main(String[] args) throws IOException {
		int ARRAY_LENGTH = 2000;
		int[] index = new int[ARRAY_LENGTH];
		String[] last_name = new String[ARRAY_LENGTH];
		String[] first_name = new String[ARRAY_LENGTH];
		String[] street_number = new String[ARRAY_LENGTH];
		String[] street_name = new String[ARRAY_LENGTH];
		String[] city = new String[ARRAY_LENGTH];
		String[] state = new String[ARRAY_LENGTH];
		String[] zip = new String[ARRAY_LENGTH];
		String[] phone = new String[ARRAY_LENGTH];
		String string_from_data;
		char comma = ',';
		int count = 0;
		int returned_index;
		String line;
		String index_string;
			try{
				BufferedReader in = new BufferedReader(new FileReader("c:\\temp\\data.txt"));	 
				while ((string_from_data = in.readLine()) != null) {//checking for end of file
					line = string_from_data;
					
					index_string = line.substring(0, (line.indexOf(comma)));
					index[count] = Integer.parseInt(index_string);
						
					line = line.substring((line.indexOf(comma) + 1), line.length());
						
					last_name[count] = line.substring(0, (line.indexOf(comma)));
						
					line = line.substring((line.indexOf(comma) + 1), line.length());
						
					first_name[count] = line.substring(0, (line.indexOf(comma)));
						
					line = line.substring((line.indexOf(comma) + 1), line.length());
						
					street_number[count] = line.substring(0, (line.indexOf(comma)));
						
					line = line.substring((line.indexOf(comma) +1 ), line.length());
					   
					street_name[count] = line.substring(0, (line.indexOf(comma)));
						
					line = line.substring((line.indexOf(comma)+1), line.length());
						
					city[count] = line.substring(0, (line.indexOf(comma)));
						
					line = line.substring((line.indexOf(comma) + 1), line.length());
						
					state[count] = line.substring(0, (line.indexOf(comma)));
						
					line = line.substring((line.indexOf(comma)+1), line.length());
						
					zip[count] = line.substring(0, (line.indexOf(comma)));
						
					line = line.substring((line.indexOf(comma) +1 ), line.length());
					   
					phone[count] = line.substring(0, (line.length()));
						
					count++;
				}//end of read line while loop while
				in.close(); 
		  }// end of try
			catch(IOException ioe){
				System.out.println("Caught I/O Exception: " + ioe.getMessage());
			}// end of catch
		
		  sort(index, last_name, first_name, street_number, street_name, city, state, zip, phone, count);
		  returned_index  = search(index, count);
		  JOptionPane.showMessageDialog(null, "The person you are looking for is:\n" +
				  first_name[returned_index] + " " + last_name[returned_index] + "\n" +
				  street_number[returned_index] + " " + street_name[returned_index] + "\n" + 
				  city[returned_index] + " " + state[returned_index] + ", " + zip[returned_index]);
	}//end of main method
	

	private static void sort(int[] index, String[] last_name, String[] first_name, String[] street_number, String[] street_name, String[] city, String[] state, String[] zip, String[] phone, int count) {
	  for (int i = index.length - 1; i >= 1; i--) {
		  double currentMax = index[0];
		  int currentMaxIndex = 0;
		  
	  }  
	}//end of sort method

	private static int search(int[] index, int count) {
	   String st = JOptionPane.showInputDialog("Enter the index of the person you are looking for:");
		int num = Integer.parseInt(st);
		int low = num;
		int high = count - 1;
		while (high >= low) {
			int mid = (low + high) / 2;
			if (count < index[mid])
				high = mid -1;
			else if (count == index[mid])
				return mid;
			else
				low = mid + 1;
			}
		return -low - 1;
	}//end of search method

   
}//end of main class



Is This A Good Question/Topic? 0
  • +

Replies To: Sort/Search Methods

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Sort/Search Methods

Posted 18 May 2008 - 09:06 PM

The code snippets has examples of sorts of all sorts
Was This Post Helpful? 0
  • +
  • -

#3 Runesmith  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 07-March 08

Re: Sort/Search Methods

Posted 19 May 2008 - 12:13 PM

Thanks, PBL! I got the BubbleSort (and the rest of the code, too) to not show any errors in my compiler, but the program doesn't seem to want to compile, for some reason. I'm not getting any specific errors messages; it just says that the build failed. I think there might an error in the Main class code that my programming teacher gave me, but he hasn't gotten back to me yet. Could anyone assist me in the mean time? Thanks!

package readfilebreakuptoarrays;
import java.io.*;

import javax.swing.JOptionPane;
public class Main {  
	public static void main(String[] args) throws IOException {
		int ARRAY_LENGTH = 2000;
		int[] index = new int[ARRAY_LENGTH];
		String[] last_name = new String[ARRAY_LENGTH];
		String[] first_name = new String[ARRAY_LENGTH];
		String[] street_number = new String[ARRAY_LENGTH];
		String[] street_name = new String[ARRAY_LENGTH];
		String[] city = new String[ARRAY_LENGTH];
		String[] state = new String[ARRAY_LENGTH];
		String[] zip = new String[ARRAY_LENGTH];
		String[] phone = new String[ARRAY_LENGTH];
		String string_from_data;
		char comma = ',';
		int count = 0;
		int returned_index;
		String line;
		String index_string;
			try{
				BufferedReader in = new BufferedReader(new FileReader("C:\\Temp\\Data.txt"));	 
				while ((string_from_data = in.readLine()) != null) {//checking for end of file
					line = string_from_data;
					
					index_string = line.substring(0, (line.indexOf(comma)));
					index[count] = Integer.parseInt(index_string);
						
					line = line.substring((line.indexOf(comma) + 1), line.length());
						
					last_name[count] = line.substring(0, (line.indexOf(comma)));
						
					line = line.substring((line.indexOf(comma) + 1), line.length());
						
					first_name[count] = line.substring(0, (line.indexOf(comma)));
						
					line = line.substring((line.indexOf(comma) + 1), line.length());
						
					street_number[count] = line.substring(0, (line.indexOf(comma)));
						
					line = line.substring((line.indexOf(comma) +1 ), line.length());
					   
					street_name[count] = line.substring(0, (line.indexOf(comma)));
						
					line = line.substring((line.indexOf(comma)+1), line.length());
						
					city[count] = line.substring(0, (line.indexOf(comma)));
						
					line = line.substring((line.indexOf(comma) + 1), line.length());
						
					state[count] = line.substring(0, (line.indexOf(comma)));
						
					line = line.substring((line.indexOf(comma)+1), line.length());
						
					zip[count] = line.substring(0, (line.indexOf(comma)));
						
					line = line.substring((line.indexOf(comma) +1 ), line.length());
					   
					phone[count] = line.substring(0, (line.length()));
						
					count++;
				}//end of read line while loop while
				in.close(); 
		  }// end of try
			catch(IOException ioe){
				System.out.println("Caught I/O Exception: " + ioe.getMessage());
			}// end of catch
		
		  sort(index, last_name, first_name, street_number, street_name, city, state, zip, phone, count);
		  returned_index  = search(index, count);
		  JOptionPane.showMessageDialog(null, "The person you are looking for is:\n" +
				  first_name[returned_index] + " " + last_name[returned_index] + "\n" +
				  street_number[returned_index] + " " + street_name[returned_index] + "\n" + 
				  city[returned_index] + " " + state[returned_index] + ", " + zip[returned_index]);
	}//end of main method
	

	private static void sort(int[] index, String[] last_name, String[] first_name, String[] street_number, String[] street_name, String[] city, String[] state, String[] zip, String[] phone, int count) {
	  int newLowest = 0;			// index of first comparison 
	int newHighest = index.length-1;  // index of last comparison 
   
	while (newLowest < newHighest) { 
		int highest = newHighest; 
		int lowest  = newLowest; 
		newLowest = index.length;	// start higher than any legal index 
		for (int i=lowest; i<highest; i++) { 
			if (index[i] > index[i+1]) { 
			   // exchange elements 
			   int temp = index[i];  index[i] = index[i+1];  index[i+1] = temp; 
			   if (i<newLowest) { 
				   newLowest = i-1; 
				   if (newLowest < 0) { 
					   newLowest = 0; 
				   } 
			   } else if (i>newHighest) { 
				   newHighest = i+1; 
			   } 
			} 
		} 
	} 
}//end method bubbleSort4


	private static int search(int[] index, int count) {
	   String st = JOptionPane.showInputDialog("Enter the index of the person you are looking for:");
		int num = Integer.parseInt(st);
		int low = num;
		int high = count - 1;
		while (high >= low) {
			int mid = (low + high) / 2;
			if (count < index[mid])
				high = mid -1;
			else if (count == index[mid])
				return mid;
			else
				low = mid + 1;
			}
		return -low - 1;
	}//end of search method

   
}//end of main class

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Sort/Search Methods

Posted 19 May 2008 - 08:29 PM

for (int i=lowest; i<highest; i++) { 
	if (index[i] > index[i+1]) { 
	// exchange elements 
		int temp = index[i];  index[i] = index[i+1];  index[i+1] = temp; 
	} 
}//end method bubbleSort4



You are only swapping the index
you have to swap all elements as your comment says :)

for (int i=lowest; i<highest; i++) { 
	if (index[i] > index[i+1]) { 
	// exchange elements 
		int temp = index[i];  index[i] = index[i+1];  index[i+1] = temp; 
					String tmpStr = lastName[i];  lastName[i] = lastName[i+1]; lastName[i+1] = tempStr;
					tmpStr = firstName[i];  firstName[i] = firstName[i+1]; firstName[i+1] = tempStr;
					..........

	} 
}//end method bubbleSort4


Was This Post Helpful? 0
  • +
  • -

#5 Runesmith  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 07-March 08

Re: Sort/Search Methods

Posted 19 May 2008 - 09:16 PM

Ack. Can't believe I forgot about swapping the indexes, especially considering I even have it marked down in the code as a reminder. Thanks again for the help. :)

Sorting is working fine now, but I'm getting a run-time error with the search function. My compiler isn't showing any errors, though. I don't really understand what it means, but here's the message I get when I run it:


Exception in thread "main" java.lang.NumberFormatException: For input string: "Armstrong" //Armstrong is the name I searched for.
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:447)
at java.lang.Integer.parseInt(Integer.java:497)
at readfilebreakuptoarrays.Main.search(Main.java:108)
at readfilebreakuptoarrays.Main.main(Main.java:72)

And the search method code:
  private static int search(int[] index, int count) {
	   String st = JOptionPane.showInputDialog("Enter the index of the person you are looking for:");
		int num = Integer.parseInt(st);
		int low = num;
		int high = count - 1;
		while (high >= low) {
			int mid = (low + high) / 2;
			if (count < index[mid])
				high = mid -1;
			else if (count == index[mid])
				return mid;
			else
				low = mid + 1;
			}
		return -low - 1;
	}//end of search method


Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Sort/Search Methods

Posted 19 May 2008 - 09:24 PM

View PostRunesmith, on 19 May, 2008 - 09:16 PM, said:

Ack. Can't believe I forgot about swapping the indexes, especially considering I even have it marked down in the code as a reminder. Thanks again for the help. :)

Sorting is working fine now, but I'm getting a run-time error with the search function. My compiler isn't showing any errors, though. I don't really understand what it means, but here's the message I get when I run it:


Exception in thread "main" java.lang.NumberFormatException: For input string: "Armstrong" //Armstrong is the name I searched for.
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:447)
at java.lang.Integer.parseInt(Integer.java:497)
at readfilebreakuptoarrays.Main.search(Main.java:108)
at readfilebreakuptoarrays.Main.main(Main.java:72)

And the search method code:
  private static int search(int[] index, int count) {
	   String st = JOptionPane.showInputDialog("Enter the index of the person you are looking for:");
		int num = Integer.parseInt(st);
		int low = num;
		int high = count - 1;
		while (high >= low) {
			int mid = (low + high) / 2;
			if (count < index[mid])
				high = mid -1;
			else if (count == index[mid])
				return mid;
			else
				low = mid + 1;
			}
		return -low - 1;
	}//end of search method



Your JOptionPane prompts for an Index which is a NUMBER.
You probably entered "Armstrong".
When to tried to convert "Armstrong" to a number by doing:
int num = Integer.parseInt(st);
Integer.parseInt() failed trying to convert "Armstrong" to an int
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1