从文本文件中读取字符串和整数输入
Reading String and Integer input from text file
我正在使用 BufferedReader
阅读一个文本文件。文本文件的形式为 (String, String, Integer)。我正在使用 inFile.readLine()
方法读取每一行并转换为 String
。我想我可以使用 Integer.parse
提取其中的 Integer
部分,但它不允许我使用 nextLine()
,我想是因为那是 Scanner
而不是 BufferedReader
.
这是我拥有的:
try{
String nextLine;
String title = "";
String department = "";
int year;
BufferedReader inFile = new BufferedReader(new FileReader(inputName));
while((nextLine = inFile.readLine())!= null){
title = nextLine.nextLine();
department = nextLine.nextLine();
year = Integer.parseInt(nextLine);
newList.addCourse(new Course(title, department, year));
}
inFile.close();
}
有人知道我应该如何阅读文本文件的 String
部分吗?
最好使用扫描仪来完成您的任务。但如果Title或department有空格,你需要指定一些分隔符。
try{
String nextLine;
String title = "";
String department = "";
int year;
BufferedReader inFile = new BufferedReader(new FileReader(inputName));
Scanner in = new Scanner(inFile);
while((nextLine = inFile.readLine())!= null){
title = in.next(); //if title has spaces, you need to use next(Pattern) instead or delimiter
department = in.next(); //if department has spaces, you need to use next(Pattern) instead or delimiter
year = in.nextInt();
newList.addCourse(new Course(title, department, year));
}
inFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
nextLine
是String,String没有nextLine
方法。但是,在这种情况下,为什么要在已分配给下一行的字符串上调用 nextLine
while((nextLine = inFile.readLine())!= null)
如果您的信息总是由 3 行组成(职位、部门和年份),那么您可以这样做:
while((nextLine = inFile.readLine())!= null){
title = nextLine;
department = inFile.readLine();
year = Integer.parseInt(inFile.readLine());
newList.addCourse(new Course(title, department, year));
}
但是,请确保它们始终在 3 行一组,否则会遇到异常。
最好是将所有 3 个数据放在同一行上,用定界符分隔,这样您就可以简单地执行以下操作:
String[] info;
while((nextLine = inFile.readLine()) != null)
{
info = nextLine.split(";");//; would be the delimiter
title = info[0];
department = info[1];
year = Integer.parseInt(info[2]);
}
我正在使用 BufferedReader
阅读一个文本文件。文本文件的形式为 (String, String, Integer)。我正在使用 inFile.readLine()
方法读取每一行并转换为 String
。我想我可以使用 Integer.parse
提取其中的 Integer
部分,但它不允许我使用 nextLine()
,我想是因为那是 Scanner
而不是 BufferedReader
.
这是我拥有的:
try{
String nextLine;
String title = "";
String department = "";
int year;
BufferedReader inFile = new BufferedReader(new FileReader(inputName));
while((nextLine = inFile.readLine())!= null){
title = nextLine.nextLine();
department = nextLine.nextLine();
year = Integer.parseInt(nextLine);
newList.addCourse(new Course(title, department, year));
}
inFile.close();
}
有人知道我应该如何阅读文本文件的 String
部分吗?
最好使用扫描仪来完成您的任务。但如果Title或department有空格,你需要指定一些分隔符。
try{
String nextLine;
String title = "";
String department = "";
int year;
BufferedReader inFile = new BufferedReader(new FileReader(inputName));
Scanner in = new Scanner(inFile);
while((nextLine = inFile.readLine())!= null){
title = in.next(); //if title has spaces, you need to use next(Pattern) instead or delimiter
department = in.next(); //if department has spaces, you need to use next(Pattern) instead or delimiter
year = in.nextInt();
newList.addCourse(new Course(title, department, year));
}
inFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
nextLine
是String,String没有nextLine
方法。但是,在这种情况下,为什么要在已分配给下一行的字符串上调用 nextLine
while((nextLine = inFile.readLine())!= null)
如果您的信息总是由 3 行组成(职位、部门和年份),那么您可以这样做:
while((nextLine = inFile.readLine())!= null){
title = nextLine;
department = inFile.readLine();
year = Integer.parseInt(inFile.readLine());
newList.addCourse(new Course(title, department, year));
}
但是,请确保它们始终在 3 行一组,否则会遇到异常。
最好是将所有 3 个数据放在同一行上,用定界符分隔,这样您就可以简单地执行以下操作:
String[] info;
while((nextLine = inFile.readLine()) != null)
{
info = nextLine.split(";");//; would be the delimiter
title = info[0];
department = info[1];
year = Integer.parseInt(info[2]);
}