在java中使用Scanner从文件中读取一些记录

问题描述

我有一个文件,里面存储了一些关于人的信息,比如姓名、姓氏、id 和...。我的程序会让用户输入一个 id,然后搜索文件以找到输入的 id,然后打印该 id 所有者的其他信息。文件信息的格式如下:性别(2个空格)姓名(2个空格)姓(2个空格)研究领域(2个空格)id(2个空格)出生日期(2个空格)出生月份(2个)空格)出生年份(然后转到下一行获取其他人的信息)。所有信息都存储为字符串。问题是当扫描仪到达 name=scan.next() 时,抛出 NoSuchElementException。顺便说一下,信息是用波斯语写的(从右到左)

public void search(String choice) { 

    String gender;
    String name;
    String lastName;
    String id;
    String fieldOfStudy;
    String date;
    String month;
    String year;
    
try {
    
    scan=new Scanner(Paths.get("C:\\Users\\Desktop\\Records.txt"));
    
    while(scan.hasNext()) {
        
        gender=scan.next();
        name=scan.next();
        lastName=scan.next();
        id=scan.next();
        fieldOfStudy=scan.next().concat(scan.next());
        date=scan.next();
        month=scan.next();
        year=scan.next();
//here I'm trying to read the information of people one by one
// then check if the entered id matches with the id of the person whose information has been read.
//(I didn't bring that piece of code here)
}catch(Exception e){ e.printStackTrace(); } }}

提前致谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)