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

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

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

  1. public void search(String choice) {
  2. String gender;
  3. String name;
  4. String lastName;
  5. String id;
  6. String fieldOfStudy;
  7. String date;
  8. String month;
  9. String year;
  10. try {
  11. scan=new Scanner(Paths.get("C:\\Users\\Desktop\\Records.txt"));
  12. while(scan.hasNext()) {
  13. gender=scan.next();
  14. name=scan.next();
  15. lastName=scan.next();
  16. id=scan.next();
  17. fieldOfStudy=scan.next().concat(scan.next());
  18. date=scan.next();
  19. month=scan.next();
  20. year=scan.next();
  21. //here I'm trying to read the information of people one by one
  22. // then check if the entered id matches with the id of the person whose information has been read.
  23. //(I didn't bring that piece of code here)
  24. }catch(Exception e){ e.printStackTrace(); } }}

提前谢谢。

暂无答案!

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

相关问题