0
我很努力想知道爲什麼這不起作用。 這意味着要求名稱的第一個字母,並根據匹配字符返回結果。 TXT文件有許多名字設立這樣的:Java:使用字符搜索文件 - 掃描儀
- 埃迪·科克倫1938至1960年
- 克里夫頓1938年至1960年
- 克里夫頓1938年至1960年
- 詹姆斯·迪恩1938年至1960年1968年至1970年1978年 - 1980
目前它只是列出所有的名字和日期。任何人都可以給我一些建議,謝謝。
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class NameYear {
public static void main(String[] args) throws FileNotFoundException
{
Scanner keyboard = new Scanner(System.in);
System.out.print("What is the first letter? ");
String input = keyboard.next().toLowerCase();
char firstLetter = input.charAt(0);
File file = new File("names.txt");
Scanner input = new Scanner(file);
while(input.hasNext())
{
String firstName = input.next();
String surname = input.next();
String year = input.nextLine();
if(surname.charAt(0) == firstLetter);
{
System.out.println(firstName + " " + surname + year);
}
}
input.close();
}
}
您在while循環中調用input.close。 – 2012-09-09 08:59:01
錯字:|代碼使用input.close();不在while循環中。 – Nicholas
@Aleksandar請把你的評論變成一個答案,@尼古拉斯,如果問題是'關閉'請接受亞歷山大的回答。 – 2012-09-09 09:08:25