【发布时间】:2016-02-20 01:46:36
【问题描述】:
我有一个来自文本文件的名称列表:MARY、PATRICIA、LINDA、BARBARA、ELIZABETH、JENNIFER、MARIA,我将它们放入一个字符串数组中。我希望能够搜索数组,但我遇到了问题。我想使用快速排序算法对其进行排序,并使用二进制搜索进行搜索,但我试图让一些简单的工作。如果我搜索“MARY”,结果将是“MARY is not it”
public class NameSearch {
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
FileReader woMen = new FileReader("names.txt");
String[] womenArray;
womenArray = new String[64];
BufferedReader reader = new BufferedReader(woMen);
String line = null;
for (int j=0;j<womenArray.length;j++)
{
womenArray[j] = reader.readLine();
}
reader.close();
int x = 0, y =0;
System.out.println("Name?");
Scanner keyboard = new Scanner(System.in);
String input = keyboard.nextLine();
for(int i=0;i<womenArray.length;i++) {
if (input == womenArray[i]) {
System.out.println("Found! "+input);
}
System.out.println(womenArray[i]+" is not it");
}
}
}
【问题讨论】: