扫描文本文件时如何跳过前导字符串?
How to skip a leading string when scanning text file?
我正在制作一个程序,使用文本文件中的指令绘制基本图像。指令格式为:
SIZE 1000 500
// GROUND
LINE 0 350 1000 350
LINE 0 351 1000 351
LINE 0 352 1000 352
LINE 0 353 1000 353
这是我的代码:
public void start(Stage stage) {
int fwidth = 0;
int fheight = 0;
try {
Scanner obj = new Scanner(new File("scene.txt"));
while(obj.hasNextLine()){
String str = obj.nextLine();
if(str.contains("SIZE")){
String a = "SIZE";
obj.skip(a);
System.out.println('b');
fwidth = obj.nextInt();
fheight = obj.nextInt();
}
if(str.contains("LINE")){
obj.skip("LINE");
System.out.println('a');
}
}
这给出了 NoSuchElementException。我假设这是因为 fwidth 和 fheight 将前导字符串作为整数但我无法弄清楚如何让扫描仪在开头跳过字符串并在知道它是什么类型的指令后读取数字.感谢任何帮助
您可以更轻松地完成以下操作:
String str = obj.nextLine();
String[] arr = str.split("\s+");// Split on whitespace
if("SIZE".equals(arr[0])) {
fwidth = Integer.parseInt(arr[1]);
fheight = Integer.parseInt(arr[2]);
} else if("LINE".equals(arr[0])) {
//...
}
如评论中所述,您可以使用 obj.next()
逐字扫描行而不是使用 nextLine()
:
Scanner obj = new Scanner(new File("scene.txt"));
int fwidth, fheight;
int num1, num2, num3, num4;
while(obj.hasNextLine() && obj.hasNext()){
String str = obj.next();
if(str.contains("SIZE")){
String a = "SIZE";
fwidth = obj.nextInt();
fheight = obj.nextInt();
System.out.println("fwidth : " + fwidth + ", fheight : " + fheight);
} else if(str.contains("LINE")){
num1 = obj.nextInt();
num2 = obj.nextInt();
num3 = obj.nextInt();
num4 = obj.nextInt();
System.out.println("num1 : " + num1 + ", num2 : " + num2 + ", num3: " + num3 + ", num4: " + num4);
}
}
我用您提供的文件对此进行了测试,它似乎有效:
src : $ java ScannerLeading
b
fwidth : 1000, fheight : 500
num1 : 0, num2 : 350, num3: 1000, num4: 350
num1 : 0, num2 : 351, num3: 1000, num4: 351
num1 : 0, num2 : 352, num3: 1000, num4: 352
num1 : 0, num2 : 353, num3: 1000, num4: 353
几个建议:
首先,我不认为
Scanner.skip()
如您所愿。 .skip() 方法的目的是告诉扫描仪在读取时“跳过”行,而不是跳过您当前所在的行。下次您调用 .nextLine() 时无论如何都会这样做。
我会完全删除您对 .skip() 的所有调用。此外,这更像是一种偏好,但我会使用 switch 语句而不是多个 if。它使您的代码更具可读性。
其次,正如约翰尼在评论中提到的那样,使用 .split() 可能会更好,因为根据我的经验,.nextInt() 会产生意想不到的结果。因此,您的代码将如下所示:
while(obj.hasNextLine()){
String[] strArray = obj.nextLine().split(" ");
switch(strArray[0]){
case "SIZE":
fwidth = Integer.parseInt(strArray[1]);
fheight = Integer.parseInt(strArray[2]);
break;
case "LINE":
//do nothing
break;
}
}
我正在制作一个程序,使用文本文件中的指令绘制基本图像。指令格式为:
SIZE 1000 500
// GROUND
LINE 0 350 1000 350
LINE 0 351 1000 351
LINE 0 352 1000 352
LINE 0 353 1000 353
这是我的代码:
public void start(Stage stage) {
int fwidth = 0;
int fheight = 0;
try {
Scanner obj = new Scanner(new File("scene.txt"));
while(obj.hasNextLine()){
String str = obj.nextLine();
if(str.contains("SIZE")){
String a = "SIZE";
obj.skip(a);
System.out.println('b');
fwidth = obj.nextInt();
fheight = obj.nextInt();
}
if(str.contains("LINE")){
obj.skip("LINE");
System.out.println('a');
}
}
这给出了 NoSuchElementException。我假设这是因为 fwidth 和 fheight 将前导字符串作为整数但我无法弄清楚如何让扫描仪在开头跳过字符串并在知道它是什么类型的指令后读取数字.感谢任何帮助
您可以更轻松地完成以下操作:
String str = obj.nextLine();
String[] arr = str.split("\s+");// Split on whitespace
if("SIZE".equals(arr[0])) {
fwidth = Integer.parseInt(arr[1]);
fheight = Integer.parseInt(arr[2]);
} else if("LINE".equals(arr[0])) {
//...
}
如评论中所述,您可以使用 obj.next()
逐字扫描行而不是使用 nextLine()
:
Scanner obj = new Scanner(new File("scene.txt"));
int fwidth, fheight;
int num1, num2, num3, num4;
while(obj.hasNextLine() && obj.hasNext()){
String str = obj.next();
if(str.contains("SIZE")){
String a = "SIZE";
fwidth = obj.nextInt();
fheight = obj.nextInt();
System.out.println("fwidth : " + fwidth + ", fheight : " + fheight);
} else if(str.contains("LINE")){
num1 = obj.nextInt();
num2 = obj.nextInt();
num3 = obj.nextInt();
num4 = obj.nextInt();
System.out.println("num1 : " + num1 + ", num2 : " + num2 + ", num3: " + num3 + ", num4: " + num4);
}
}
我用您提供的文件对此进行了测试,它似乎有效:
src : $ java ScannerLeading
b
fwidth : 1000, fheight : 500
num1 : 0, num2 : 350, num3: 1000, num4: 350
num1 : 0, num2 : 351, num3: 1000, num4: 351
num1 : 0, num2 : 352, num3: 1000, num4: 352
num1 : 0, num2 : 353, num3: 1000, num4: 353
几个建议:
首先,我不认为
Scanner.skip()
如您所愿。 .skip() 方法的目的是告诉扫描仪在读取时“跳过”行,而不是跳过您当前所在的行。下次您调用 .nextLine() 时无论如何都会这样做。
我会完全删除您对 .skip() 的所有调用。此外,这更像是一种偏好,但我会使用 switch 语句而不是多个 if。它使您的代码更具可读性。
其次,正如约翰尼在评论中提到的那样,使用 .split() 可能会更好,因为根据我的经验,.nextInt() 会产生意想不到的结果。因此,您的代码将如下所示:
while(obj.hasNextLine()){
String[] strArray = obj.nextLine().split(" ");
switch(strArray[0]){
case "SIZE":
fwidth = Integer.parseInt(strArray[1]);
fheight = Integer.parseInt(strArray[2]);
break;
case "LINE":
//do nothing
break;
}
}