所以我有一个文件,其中包含所有的总统 - 他们的名字,中间名首字母(如果有的话)和姓氏.
需要读入该文件,并且用户可以输入总统的名称来搜索它,并且应该显示该总统.
如果用户按名字或姓氏搜索,而不是两者都搜索,我会显示总统.
例如,外部文件包含:
George,Washington,(1789-1797)
Franklin,D.,Roosevelt,(1933-1945)
... and so on with all the presidents
Run Code Online (Sandbox Code Playgroud)
我需要用户能够键入名字,姓氏,或者姓名和名字,并获得所需的结果(日期与大部分时间无关).
试过很多不同的事情,但如果用户按名字和姓氏搜索,就不能到达显示总统的地方.
这是我到目前为止所得到的:
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<String> presidentsArrayList = new ArrayList<String>();
// prompt user to enter a string to see if it …Run Code Online (Sandbox Code Playgroud)