使用扫描仪逐行读取文件,并根据数据创建对象

时间:2019-05-18 02:05:52

标签: java file-io

我的任务是使用扫描仪逐行读取文件并从每一行创建Student对象。

这是文本文件的布局:

id,first_name,last_name,username,password,UserType,permission,course,degree
35,Bartholom,Sapsedidiy,wbeeseb3,Q8yPvK4qm,Student,Reserve,EngineerI,Legal

我确保每个属性都以正确的顺序进入新创建的学生对象。

我试图在每个逗号后获取每个下一个字符串或整数,并将其分配给相关属性,即:

try
    {
        input = new Scanner(Paths.get("C:\\Users\\Home\\Desktop\\Student.txt"));
        input.useDelimiter(",");

        while(input.hasNext()) {
            int id = input.nextInt();
            String firstName = input.next();
            String lastName = input.next();
            String userName = input.next();
            String passWord = input.next();
            String type = input.next();
            enumUserType userType = enumUserType.valueOf(type.toUpperCase());
            String permission = input.next();
            enumPermissionType permissionType = enumPermissionType.valueOf(permission.toUpperCase());
            String course = input.next();
            String degree = input.next();

            Address userAddress = new Address(1, 23, "Fake street", "Suburb1", 2500, enumState.NSW);
            User user = new Student(id, firstName, lastName, userName, passWord, userType, permissionType, true, course, degree, userAddress);
            getLibrary().addUser(user);

        }
    }
    catch(IOException io)
    {
        System.out.println("Error opening file");
    }

事情马上就发生了,我在int id上得到一个输入不匹配异常,这是第一个读取值,我不确定这里出了什么问题,我需要一些帮助来解决此错误,

我对学习文件读/写只是一个新手。 也许我按错误的顺序排列某些东西,或者它以某种方式读取了错误的值?

提前谢谢!

编辑: 我的错误消息是:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at library.Library.initData(Library.java:262)
at library.Library.main(Library.java:225)

第262行是:int id = input.nextInt();

1 个答案:

答案 0 :(得分:1)

尝试一下-

mvn -version

或者,也可以使用扫描仪来实现。

try
    {
        input = new Scanner(Paths.get("C:\\Users\\Home\\Desktop\\Student.txt"));
        while(input.hasNextLine()) {
            String[] line = input.nextLine().split(",");
            int id = Integer.parseInt(line[0]);
            String firstName = line[1];
            String lastName = line[2];
            String userName = line[3];
            String passWord = line[4];
            String type = line[5];
            enumUserType userType = enumUserType.valueOf(type.toUpperCase());
            String permission = line[6];
            enumPermissionType permissionType = enumPermissionType.valueOf(permission.toUpperCase());
            String course = line[7];
            String degree = line[8];

            Address userAddress = new Address(1, 23, "Fake street", "Suburb1", 2500, enumState.NSW);
            User user = new Student(id, firstName, lastName, userName, passWord, userType, permissionType, true, course, degree, userAddress);
            getLibrary().addUser(user);

        }
    }
    catch(IOException io)
    {
        System.out.println("Error opening file");
    }

但是上一个是我的首选方式。