使用扫描仪逐行读取文件并从数据中创建对象
Reading from a file using scanner, line by line and creating an object out of the data
我的任务是使用扫描仪逐行读取文件并从每一行创建学生对象。
这是文本文件的布局:
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 上收到了一个输入不匹配异常,这是第一个读取值,我不确定这里出了什么问题,我需要一些帮助来修复这个错误,
我只是刚开始学习 read/write 档案。
也许我有一些错误的顺序或者它读取了错误的值?
提前致谢!
编辑:
我的错误信息是:
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();
试试这个 -
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");
}
或者,这也可以使用扫描仪来实现。
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");
}
但我更喜欢前一种方式。
我的任务是使用扫描仪逐行读取文件并从每一行创建学生对象。
这是文本文件的布局:
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 上收到了一个输入不匹配异常,这是第一个读取值,我不确定这里出了什么问题,我需要一些帮助来修复这个错误,
我只是刚开始学习 read/write 档案。 也许我有一些错误的顺序或者它读取了错误的值?
提前致谢!
编辑: 我的错误信息是:
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();
试试这个 -
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");
}
或者,这也可以使用扫描仪来实现。
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");
}
但我更喜欢前一种方式。