英文:
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
问题
以下是翻译好的内容:
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.equals("add")) {
double thistotal = number + numberTwo;
total += thistotal;
}
}
System.out.println(equation);
} catch (FileNotFoundException ex) {
System.out.println("无法找到文件");
} catch (InputMismatchException e) {
}
return 0;
}
}
英文:
The document I'm scanning says "enter(10);add;(45.76)" on a single line. It's supposed to read over the parenthesis and semicolons and just get the numbers and strings, as well as read the word "enter" before running the code. It manages to read enter correctly and the first number, but after that when scanning for "equation" it instead grabs 45.67) with the parenthesis. If i remove the (45.67) and leave just add; it works and grabs the add. I'm not sure what's going on wrong here. Any help would be appreciated, as well as any advice on how to get this program to scan the next line in the file if there was another one.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
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;
}
}
<!-- end snippet -->
答案1
得分: 0
以下是翻译好的内容:
在您的示例代码中存在一些问题:
首先,在 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();
}
}
}
希望这有所帮助!
英文:
There are few issues in your sample code:
Firstly in java you can not compare string with == you should use Equals method to check equality.
Secondly operation should be done after reading entire line,
also there were blank chars were read by scanner we need to handle that
Please have look into below working code and verify the output.
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();
}
}
}
Hope this helps!
专注分享java语言的经验与见解,让所有开发者获益!
评论