提问人:BRuhCodR 提问时间:4/6/2020 更新时间:4/6/2020 访问量:139
为什么我的 parser.next 扫描仪会选择括号和数字,而不是 i 之前的字符串,尽管有一个分隔符
Why is my parser.next scanner picking up on a parenthesis and number instead of the string that comes before i despite having a delimiter
问:
我正在扫描的文档显示“enter(10);加;(45.76)“在一行上。它应该阅读括号和分号,只获取数字和字符串,并在运行代码之前阅读单词“enter”。它设法正确读取回车和第一个数字,但之后在扫描“方程式”时,它反而抓住了带有括号的 45.67)。如果我删除 (45.67) 并留下 add ; 它可以工作并获取添加。我不确定这里出了什么问题。任何帮助将不胜感激,以及有关如何让该程序扫描文件中下一行(如果有另一行)的任何建议。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
import java.util.Scanner;
public class CPLParser {
public double parseScript(String inputFile) throws CPLException{
File file = new File(inputFile);
try (Scanner Filereader = new Scanner(file)){
String line = Filereader.nextLine();
Scanner parser = new Scanner(line);
parser.useDelimiter("\\(|\\)\\;");
String enter = parser.next();
double number = 0;
String equation = " ";
double numberTwo = 0;
double total = 0;
if (!enter.equals("enter")){
throw new InvalidProgramStartException("");
}
while (parser.hasNext()) {
if (parser.hasNextDouble()){
number = parser.nextDouble();
}
if (parser.hasNext()){
equation = parser.next();
}
if (parser.hasNextDouble()){
numberTwo = parser.nextDouble();
}
if (equation == "add") {
double thistotal = number + numberTwo;
total += thistotal;
}
}
System.out.println(equation);
} catch (FileNotFoundException ex) {
System.out.println("Could not find the file");
} catch (InputMismatchException e) {
}
return 0;
}
}
答:
0赞
Programmer Analyst
4/6/2020
#1
示例代码中存在几个问题:
首先,在 java 中,你不能将字符串与 == 进行比较,你应该使用 Equals 方法来检查相等性。 其次,操作应在读取整行后进行, 此外,扫描仪读取了空白字符,我们需要处理它
请查看下面的工作代码并验证输出。
import java.net.MalformedURLException;
import java.net.URL;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.*;
import java.io.*;
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
System.out.println("Hello World");
try {
Scanner parser = new Scanner("enter(10);add;(45.76)");
parser.useDelimiter("\\(|\\)|\\;");
String enter = parser.next();
System.out.println("enter "+enter);
double number = 0;
String equation = " ";
double numberTwo = 0;
double total = 0;
if (!enter.equals("enter")){
// new InvalidProgramStartException("");
System.out.println("InvalidProgramStartException");
}
while (parser.hasNext()) {
if (parser.hasNextDouble()){
number = parser.nextDouble();
System.out.println("number "+ number);
}
if (parser.hasNext()){
String text= parser.next();
// only set equation if its not blank
if ("".equals(text))
{ System.out.println("equation is Blank "+ equation +"...");}
else
{equation = text;
System.out.println("Setting equation "+ equation);}
}
if (parser.hasNextDouble()){
numberTwo = parser.nextDouble();
System.out.println("numberTwo "+ numberTwo);
}
}
if (equation.equals("add")) {
double thistotal = number + numberTwo;
System.out.println("thistotal "+ thistotal);
System.out.println("total "+ total);
total += thistotal;
System.out.println("total "+ total);
}
System.out.println(equation);
} catch (Exception e) {
e.printStackTrace();
}
}
}
希望这有帮助!
评论
0赞
BRuhCodR
4/6/2020
非常有趣和有用,但出于某种原因,我的扫描仪仍然抓取“47.56)”作为 .nextLine(),我不确定为什么。这没有多大意义。虽然你的代码似乎工作得很好。
上一个:使用扫描仪从文件中读取格式化文本
评论