Reading from a file using scanner, line by line and creating an object out of the data

44
May 18, 2019, at 8:50 PM

I am tasked with reading a file line by line using scanner and creating Student objects from each line.

This is the layout of the text file:

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

I have made sure that each attribute is going into thenewly created student object are in the correct order.

I am trying to get each next string or int after each comma and assign it to the relevant attribute that it is:

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

The thing is straight away i get an input mismatch exception on the int id which is the first read value, i am not sure what is going wrong here and i need some help to fix this error,

i am only fairly new to learning read/write on file. Maybe i have something in the wrong order or its reading the wrong values some how?

Thank you in advance!

edit: my error message is:

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)

Line 262 is: int id = input.nextInt();

Answer 1

Try this -

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

Alternatively, this can also be achieved using Scanner.

try
    {
        input = new Scanner(Paths.get("C:\\Users\\Home\\Desktop\\Student.txt"));
        while(input.hasNextLine()) {
            Scanner line = new Scanner(input.nextLine());
            line.useDelimiter(",");
            int id = line.nextInt();
            String firstName = line.next();
            String lastName = line.next();
            String userName = line.next();
            String passWord = line.next();
            String type = line.next();
            enumUserType userType = enumUserType.valueOf(type.toUpperCase());
            String permission = line.next();
            enumPermissionType permissionType = enumPermissionType.valueOf(permission.toUpperCase());
            String course = line.next();
            String degree = line.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");
    }

But the previous one has been my preferred way.

READ ALSO
Text wrapping based on the size in Java

Text wrapping based on the size in Java

I am having the string which includes the Japanese characters and English and I want to wrap that text in java for certain pixelsI have tried with wrap option in wordutils and it is not working because the wrap length is varied based on the sentence that I'm giving

39
Same erasure on for subclass that maps parent method to a different class

Same erasure on for subclass that maps parent method to a different class

I am attempting to refactor a DAO to make it a bit more usable in our code baseWe currently have a parameterized AbstractDao that takes in three types:

25
Escape sequences in command line arguments (Java)

Escape sequences in command line arguments (Java)

According to How to use line break argument

39