使用扫描程序从txt文件中获取数据

时间:2018-02-22 16:57:14

标签: java eclipse

我想从txt文件中获取Name,Surname和birthdate 这就是我所拥有的

Scanner scFile = new Scanner (new File("BirthDates.txt"));
        String Name;
        int BirthDates = 0;
        String Line = null;

            while(scFile.hasNext())
            {
                Line = scFile.nextLine();
                Scanner scLine = new Scanner(Line).useDelimiter("#");

                Name = scLine.next();
                BirthDates = scLine.nextInt();
                scLine.close();
                System.out.println(Name + BirthDates);
            }
        scFile.close();

The errors that I get The txt File

1 个答案:

答案 0 :(得分:1)

问题来源:

在这一行:BirthDates = scLine.nextInt();,您尝试将出生日设为int,但是您在# Jackson之后读取了第二个值。

解决方案:

获取全名,然后获得您的出生日:

  Name = scLine.next();
  secondeName = scLine.next();
  BirthDates = scLine.nextInt();

有关详情,请参阅this