为什么我的parser.next扫描程序会选择括号和数字,而不是尽管有分隔符但在我之前出现的字符串

问题描述 投票:1回答:1

我正在扫描的文档在一行上说“ enter(10); add;(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;
	}

}
java java.util.scanner delimiter
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();
        }
    }
}

希望这会有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.