I have a few minor problems with this program:

1. My selectionSort method - it sorted Z - A, not A - Z.

2. Since I used a string array, I'm running into problems with "operator > cannot be applied" and "incompatible types".

3. Return "search term not found" in dialog box.

Any assistance would be greatly appreciated, even a nudge in the right direction.

Thank you

// Import packagages
import java.io.*;
import java.util.*;
import javax.swing.*;

public class StudentNamesSelectionSortBinarySearch

{	//Declare final int 
	final static int NO_OF_STUDENTS = 20;
	
	public static void main (String[] args) throws FileNotFoundException
	
	{	//Declare variables
		int i;
		int temp;
		int loc;
		String inputName;
		String[] studentNames;
	
		//Set array length
		studentNames = new String[NO_OF_STUDENTS];

		//Input data from file
		Scanner inFile = new Scanner(new FileReader("studentnames.txt"));

		//Call for getStudentNames method
		getStudentNames(inFile, studentNames);
	
		//Display dialog box with unsorted array	
		JOptionPane.showMessageDialog(null, studentNames, "Student Names - Unsorted", JOptionPane.PLAIN_MESSAGE);
		
		//Call for selectionSort method
		selectionSort(studentNames, NO_OF_STUDENTS);
	
		//Display dialog box with sorted array
		JOptionPane.showMessageDialog(null, studentNames, "Student Names - Sorted", JOptionPane.PLAIN_MESSAGE);
		
		//Display dialog box for input of search term - could not proceed beyond this point
/*		inputName = JOptionPane.showInputDialog("Enter the student's name you wish to search for and press OK");
	
		//Call for binarySearch method
		binarySearch(studentNames, NO_OF_STUDENTS, inputName);
		
		//Display dialog box for result not found
		JOptionPane.showMessageDialog(null, inputName + " is not in the class.", "Search Result", JOptionPane.PLAIN_MESSAGE);
	*/	
	}	
		
		
// getStudentNames method reads student names from file studentnames.txt and fills array studentNames

		public static void getStudentNames(Scanner inFile, String[] studentNames)
		
		{
			int i;
			for (i = 0; i < studentNames.length; i++)
				studentNames[i] = inFile.next();
				
		}
		
// sorts array studentNames using method selectionSort - sorted in reverse order, could not figure out how to correct

		public static void selectionSort(String studentNames[],int NO_OF_STUDENTS)
		{	
			int index;
			int smallestIndex;
			int minIndex;
			String temp;
			
			for (index = 0; index < NO_OF_STUDENTS - 1; index++)
			{
				smallestIndex = index;
				
				for (minIndex = index + 1; minIndex < NO_OF_STUDENTS; minIndex++)
					if (studentNames[minIndex].compareTo(studentNames[smallestIndex]) > 0)
						smallestIndex = minIndex;
					
						temp = studentNames[smallestIndex];
						studentNames[smallestIndex] = studentNames[index];
						studentNames[index] = temp;
					
			}
		}
			
// searches sorted array studentNames using method binarySearch

	public static String binarySearch(String studentNames[], int NO_OF_STUDENTS, String inputName)
		
		{	
			int first = 0;
			int last = NO_OF_STUDENTS - 1;
			int mid;
			
			boolean found = false;
			
			while (first <= last && !found)
			{
				mid = (first + last) / 2;
				
				if (studentNames[mid].equals("inputName"))
					found = true;
				else if (studentNames[mid] > inputName)
					last = mid - 1;
				else 
					first = mid + 1;
			}

				if (found)
					return mid;
				else
					return -1;
			}


}

Error messages:

StudentNamesSelectionSortBinarySearch.java:113: operator > cannot be applied to java.lang.String,java.lang.String
else if (studentNames[mid] > inputName)
^
StudentNamesSelectionSortBinarySearch.java:120: incompatible types
found : int
required: java.lang.String
return mid;
^
StudentNamesSelectionSortBinarySearch.java:122: incompatible types
found : int
required: java.lang.String
return -1;
^

Recommended Answers

All 2 Replies

Using comparison

string1 > string2

is illegal in Java. To compare Strings You use compareTo method of String class:

string1.compareTo( string2 ) > 0

what would be in illegal way:

string1 > string2

Zibo,

Thanks, that fixed one of them. I should have realized that because I used the .equals right before it.

Tracy

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.