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

axr492tv  于 2021-08-25  发布在  Java
关注(0)|答案(0)|浏览(123)

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

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(); } }}

提前谢谢。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题