java 数组名_java – 字符串数组名称搜索

所以我有一个文件,其中包含所有的总统 – 他们的名字,中间名首字母(如果有的话)和姓氏.

需要读入该文件,并且用户可以输入总统的名称来搜索它,并且应该显示该总统.

如果用户按名字或姓氏搜索,而不是两者都搜索,我会显示总统.

例如,外部文件包含:

George,Washington,(1789-1797)

Franklin,D.,Roosevelt,(1933-1945)

... and so on with all the presidents

我需要用户能够键入名字,姓氏,或者姓名和名字,并获得所需的结果(日期与大部分时间无关).

试过很多不同的事情,但如果用户按名字和姓氏搜索,就不能到达显示总统的地方.

这是我到目前为止所得到的:

public class NameSearch {

public static void main(String[] args) throws IOException {

try {

// read from presidents file

Scanner presidentsFile = new Scanner(new File("Presidents.txt"));

// scanner for user input

Scanner keyboard = new Scanner(System.in);

// create array list of each line in presidents file

ArrayList presidentsArrayList = new ArrayList();

// prompt user to enter a string to see if it matches with a president's name

System.out.println("Enter a search string of letters to find a president match: ");

// store user input

String userInput = keyboard.nextLine();

// add president file info to array list linesInPresidentFile

while (presidentsFile.hasNextLine()) {

presidentsArrayList.add(presidentsFile.nextLine());

} // end while loop

String presidentNamesArray[] = presidentsArrayList.toArray(new String[presidentsArrayList.size()]);

String results = searchArray(presidentNamesArray, userInput);

//System.out.println("\nThe presidents who have \"" + userInput + "\" as part of their name are: ");

} catch (FileNotFoundException ex) {

// print out error (if any) to screen

System.out.println(ex.toString());

} // end catch block

} // end main

// method to search for a specific value in an array

public static String searchArray(String array[], String value) {

for (int i = 0; i < array.length; i++) {

if (array[i].toLowerCase().contains(value.toLowerCase())) {

String splitter[] = array[i].split(" ,");

System.out.println(Arrays.toString(splitter));

}

}

return Arrays.toString(array);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值