我有这个程序:
public class KNW_NameSearch
{
/**
* The quickSort method, will sort the array of strings
* @param femaleNames, The array of female names.
* */
public void quickSort (int femaleNames[])
{
doQuickSort(femaleNames, 0, femaleNames.length - 1);
}
/**
* The doQuickSort method, will execute the quick sort.
* @param femaleNames, The array of female names.
* @param start, The starting subscript.
* @param end, The ending subscript.
* */
private void doQuickSort(int femaleNames[],
int start, int end)
{
//Declare pivot point
int pivotPt;
if(start > end)
{
//Get the pivot point
pivotPt = partition(femaleNames, start, end);
//Sort the first sublist
doQuickSort(femaleNames, start, pivotPt - 1);
//Sort the second sublist
doQuickSort(femaleNames, pivotPt + 1, end);
}
}
/**
* The parition method, determines the pivot value and rearranges
* the array.
* @param femaleNames, The array of female names.
* @param start, The starting subscript.
* @param end, The ending subscript.
* */
private int partition(int femaleNames[],
int start, int end)
{
int pivotValue;
int endOfLeftList;
int mid;
//Find the middle element
mid = (start + end) / 2;
//Call the swap method
swap(femaleNames, start, mid);
//Save pivotValue
pivotValue = femaleNames[start];
//Save endOfLeftList
endOfLeftList = start;
//A for-loop to sort the names in a given array
for(int x = start + 1; x <= end; x++)
{
if(femaleNames[x] < pivotValue)
{
endOfLeftList++;
//Call the swap method
swap(femaleNames, endOfLeftList, x);
}
}
swap(femaleNames, start, endOfLeftList);
return endOfLeftList;
}
/**
* The swap method, exchanging values in the provided subscripts.
* @param femaleNames, The array of femaleNames.
* @param a, A subscript to be swapped.
* @param b, A subscript to be swapepd.
* */
private void swap(int femaleNames[],
int a, int b)
{
//Perform swap
int temp;
temp = femaleNames[a];
femaleNames[a] = femaleNames[b];
femaleNames[b] = temp;
}
/**
* The binarySearch method, will perform a binary search
* on a sorted array.
* @param fNames, The array of names.
* @param names, A string of names
* */
public int binarySearch(int fNames[], int names)
{
//Return the doBinarySearch method
return doBinarySearch(fNames, 0, fNames.length - 1, names);
}
/**
* The doBinarySearch method, will execute a binary search
* @param fNames, The array of names
* @param first, The first subscript.
* @param last, The last subscript.
* @param name, A string of names
* */
private int doBinarySearch(int fNames[], int names,
int first, int last)
{
int middle;
//Return -1 if the name isn't in the array
if(first > last)
{
return -1;
}
//Find the middle position
middle = (first + last) / 2;
//Find the position of the given name
if(fNames[middle] == names)
{
return doBinarySearch(fNames, middle + 1,
last, first);
}
else if(fNames[middle] < names)
{
return doBinarySearch(fNames, first,
middle - 1, names);
}
else
{
return middle;
}
}
}
演示:
public class KNW_NameSearchDemo
{
public static void main(String args[]) throws IOException
{
//Declare the variables
int searchName;
int firstNames[] = new int[65];
String choice;
int result;
//Create the scanenr object
Scanner scanner = new Scanner(System.in);
//Create an object for KNW_NameSearch
KNW_NameSearch kns = new KNW_NameSearch();
//Open the file
File file = new File("names.txt");
Scanner name = new Scanner(file);
//Read the lines in the file until there is no more
for(int x = 0; x < 65; x++)
{
//Read the next line
firstNames[x] = name.nextInt();
}
//Print the names before sorting array
System.out.println("Before sorting: \n");
for(int x = 0; x < 65; x++)
{
System.out.println(firstNames[x]);
}
//Call to quickSort
kns.quickSort(firstNames);
//Search about a name
do
{
//Print the names after sorting array
System.out.println("\nPlease enter a name to search: ");
searchName = scanner.nextInt();
//Call to binarySearch method
result = kns.binarySearch(firstNames, searchName);
//Print the result
if(result == -1)
{
System.out.println("\n" + searchName + "wasn't found!");
}
else
{
System.out.println("\n" + searchName + "was found at position "
+ (result + 1));
}
//Does the user want to use again
System.out.print("\n-------\nDo you want to use again?: ");
choice = scanner.nextLine();
}
//if the user said yes
while(choice.charAt(0) == 'y');
}
}
我收到一个InputMismatchError,我实在无法理解。我不知道他们告诉我错误在哪里。以下是错误消息:
错误:
java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at KNW_NameSearchDemo.main(KNW_NameSearchDemo.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:267)
有谁知道是什么原因导致出现此错误消息?我试图在文本文件的列表上进行二进制搜索,之前执行快速排序。