Java 如何在已使用scan.nextLine()扫描的行中扫描整数
我正在尝试编写一个方法,该方法扫描文本文件并将所有以Java 如何在已使用scan.nextLine()扫描的行中扫描整数,java,java.util.scanner,Java,Java.util.scanner,我正在尝试编写一个方法,该方法扫描文本文件并将所有以#开头的行添加到名为metadata的字符串中。之后,我想扫描接下来的三个整数,从第一行开始,不包含。但是,不包含#的第一行被跳过,因为我已经使用了scan.nextLine()来扫描它 File file = new File(filename); Scanner scan = new Scanner(file); String format = scan.nextLine(); String metadata = ""; //This
#
开头的行添加到名为metadata的字符串中。之后,我想扫描接下来的三个整数,从第一行开始,不包含
。但是,不包含#
的第一行被跳过,因为我已经使用了scan.nextLine()
来扫描它
File file = new File(filename);
Scanner scan = new Scanner(file);
String format = scan.nextLine();
String metadata = "";
//This loop adds all lines starting with # to metadata.
outerloop:
while(scan.hasNextLine()){
String line = scan.nextLine();
if(line.startsWith("#")){
metadata = metadata+line;
} else {
break outerloop;
}
}
//Scans the next three integers and sets them equal to width, height, and maxRange.
int width = scan.nextInt();
int height = scan.nextInt();
int maxRange = scan.nextInt();
我的输入是一个文本文件。前六行显示在此屏幕截图中
只有一行以#开头,但是我的方法必须能够处理多行以#开头的文件。输出应该是
format = "P3"
metadata = "# CREATOR: GIMP PNM Filter Version 1.1"
width = 200
height = 133
maxRange = 255
然而,相反,我得到了
format = "P3"
metadata = "# CREATOR: GIMP PNM Filter Version 1.1"
width = 255
height = 183
maxRange = 187
你的台词是字符串。您可以搜索与以下正则表达式匹配的所有子字符串:
[0-9]+
这可以通过以下方式完成:
List<String> matches = new ArrayList<>();
Matcher m = Pattern.compile("[0-9]+")
.matcher(readLineWhichIsString);
while (m.find()) {
matches.add(m.group());
}
int[] numbersInLine = new int[matches.size()];
for (int i = 0; i < matches.size(); i++) {
numbersInLine[i] = Integer.parseInt(matches.get(i));
}
将只匹配那些数字后跟空格或字符串结尾的字符串
编辑:
下面的代码将用int
参数填充int[]值
String line;
while(scan.hasNextLine()){
line = scan.nextLine();
if(line.startsWith("#")){
metadata = metadata+line;
}else{
break outerloop;
}
}
int[] values = new int[3];
List<String> matches = new ArrayList<>();
Matcher m = Pattern.compile("[0-9]+(?=\\s|$)")
.matcher(readLineWhichIsString);
while (m.find()) {
matches.add(m.group());
}
int i = 0;
for (i = 0; i < Math.min(matches.size(), values.length); i++) {
numbersInLine[i] = Integer.parseInt(matches.get(i));
}
while (i < 3) {
values[i] = scan.nextInt();
}
字符串行;
while(scan.hasNextLine()){
line=scan.nextLine();
if(第行开始,以“#”号填列){
元数据=元数据+行;
}否则{
断开外环;
}
}
int[]值=新的int[3];
列表匹配项=新的ArrayList();
Matcher m=Pattern.compile(“[0-9]+(?=\\s |$)”)
.matcher(readLineWhichIsString);
while(m.find()){
匹配。添加(m.group());
}
int i=0;
对于(i=0;i
当您检查#in line时,您的问题在于逻辑,而不是您想再次阅读该行
这里有一个建议的解决方案
//This loop adds all lines starting with # to metadata.
String line = null;
while(scanner.hasNextLine()){
line = scanner.nextLine();
if(line.startsWith("#")){
metadata = metadata+line;
}else{
break ;
}
}
//Scans the next three integers and sets them equal to width, height, and maxRange.
String ints[]=line.split(" ");
int width = Integer.parseInt(ints[0]);
int height = Integer.parseInt(ints[1]);
int maxRange = scanner.nextInt();
这是有效的,你会得到想要的结果
既然你已经有了一行,为什么还要重新阅读一个文件呢?请将输入和预期输出显示为注释……你可以制作新的扫描仪(行)
我编辑了我的答案,并给了你工作代码。谢谢@cricket_007,我试过了,现在工作了:)谢谢,最后使用了新的扫描仪(行)相反,因为它更简单。@MeghanWhite建议你也尝试一下我的解决方案,因为有一天它可能会有所帮助。
//This loop adds all lines starting with # to metadata.
String line = null;
while(scanner.hasNextLine()){
line = scanner.nextLine();
if(line.startsWith("#")){
metadata = metadata+line;
}else{
break ;
}
}
//Scans the next three integers and sets them equal to width, height, and maxRange.
String ints[]=line.split(" ");
int width = Integer.parseInt(ints[0]);
int height = Integer.parseInt(ints[1]);
int maxRange = scanner.nextInt();