我相信你能说出来,但我是java的新手。 无论如何,当一个人输入一个名字时,我试图读取接下来的3行,当文件中有其他名称和信息时从txt文件中搜索。信息被放入另一个程序的txt文件中,该程序询问名字,姓氏,身份证号码和经验年限,并将它们打印在不同的行上。这是我到目前为止所做的。
System.out.println("Please enter first name of employee.");
String employeeName = keyboard.nextLine();
// Check to see if the name is in the file
// And for some reason if the name is in it, it says that it's not
if(fileName.contains(employeeName))
{
System.out.println("Sorry, that employee is not in the file.");
}
else
{
// Read the last name.
String lastName = inputFile.nextLine();
System.out.println(lastName);
// Read the employee number
String employeeID = inputFile.nextLine();
System.out.println(employeeID);
// Read the years of experience
String years = inputFile.nextLine();
System.out.println(years);
}
答案 0 :(得分:0)
您可以使用.hasNext方法读取文件中的行
这是我的伪代码:
else
{
String[] name;
int[] id;
int[] yearsofexp;
while (filename.hasNext())
{
name = filename.nextLine();
id = filename.nextInt;
yearsofexp = filename.nextInt();
}
}
答案 1 :(得分:0)
create a file "myFile.txt" and save it in the same folder with your code.
公共课Abebe { public static void main(String [] args)抛出FileNotFoundException {
Scanner keyboard= new Scanner(System.in);
System.out.println("Please enter first name of employee.");
String employeeName = keyboard.nextLine();
File fileName= new File("myFile.txt"); // open the file
Scanner inputFile= new Scanner(fileName); // read in the file
if(!inputFile.hasNext(employeeName)) // here you are checking if the name you entered is available in your file
{
System.out.println("Sorry, that employee is not in the file.");
}
else
{
// Read the last name.
String lastName = inputFile.nextLine();
System.out.println(lastName);
// Read the employee number
String employeeID = inputFile.nextLine();
System.out.println(employeeID);
// Read the years of experience
String years = inputFile.nextLine();
System.out.println(years);
}
}}