解析字符串Java中的多个数字

时间:2013-11-22 17:21:24

标签: java parsing

我的java类有一个问题,我必须解析多个.txt文件并提取数学问题字符串,并将字符串中的答案与问题的实际答案进行比较。我已经能够阅读和检查两个单位数字的问题,但我无法通过。我必须能够检查最多4个任意长度的数字以及带有6个位数的小数(0.000001)。 我也不能在这段代码中使用正则表达式或数组。

到目前为止,这是我的代码:

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Scanner;

import javax.print.attribute.standard.NumberOfDocuments;

public class CheckAnswers
{
private static double correct = 0;
private static double numQuestions = 0;

public static void Test(Object newline) throws IOException, ParseException
{
    while (((Scanner) newline).hasNext())
    {
        String line = ((Scanner) newline).nextLine();
        // String line = "8 - 5 = 3";
        //                0123456789

        String num1 = line.substring(0, 1);
        double firstNum = Integer.parseInt(num1);
        //double firstNum = NumberFormat.getInstance().parse(num1).intValue();
        String num2 = line.substring(4, 5);
        double secondNum = NumberFormat.getInstance().parse(num2).intValue();
        String answer = line.substring(8, 9);
        double userAnswer = NumberFormat.getInstance().parse(answer).intValue();

        int loopExit = 0;
        while (loopExit == 0)
        {
            int checkSubtraction = line.indexOf('-');
            if (checkSubtraction != -1)
            {
                loopExit++;
                Subtraction(line, firstNum, secondNum, userAnswer);
            }
            int checkAddition = line.indexOf('+');
            if (checkAddition != -1)
            {
                loopExit++;
                Addition(line, firstNum, secondNum, userAnswer);
            }
            int checkDivision = line.indexOf('/');
            if (checkDivision != -1)
            {
                loopExit++;
                Division(line, firstNum, secondNum, userAnswer);
            }
            int checkMultiplication = line.indexOf('*');
            if (checkMultiplication != -1)
            {
                loopExit++;
                Multiplication(line, firstNum, secondNum, userAnswer);
            }
        }
        numQuestions++;
    }
    System.out.println("Number correct: " + correct + " out of " + numQuestions);
    double percentGrade = ((correct * 100) / numQuestions);
    System.out.println("Grade: " + percentGrade + "%");
}

public static void Subtraction(String line, double firstNum, double secondNum,
        double userAnswer) throws ParseException
{
    double keyNum = firstNum - secondNum;
    if (userAnswer == keyNum)
    {
        System.out.println("Correct");
        correct++;
    }
    if (userAnswer != keyNum)
    {
        System.out.println("Wrong");
    }
}

public static void Addition(String line, double firstNum, double secondNum,
        double userAnswer) throws ParseException
{
    double keyNum = firstNum + secondNum;
    if (userAnswer == keyNum)
    {
        System.out.println("Correct");
        correct++;
    }
    if (userAnswer != keyNum)
    {
        System.out.println("Wrong");
    }
}

public static void Multiplication(String line, double firstNum, double secondNum,
        double userAnswer) throws ParseException
{
    double keyNum = firstNum * secondNum;
    if (userAnswer == keyNum)
    {
        System.out.println("Correct");
        correct++;
    }
    if (userAnswer != keyNum)
    {
        System.out.println("Wrong");
    }
}

public static void Division(String line, double firstNum, double secondNum,
        double userAnswer) throws ParseException
{
    double keyNum = firstNum / secondNum;
    if (userAnswer == keyNum)
    {
        System.out.println("Correct");
        correct++;
    }
    if (userAnswer != keyNum)
    {
        System.out.println("Wrong");
    }
}
}

1 个答案:

答案 0 :(得分:0)

RegEx是最容易使用的,因为你有可变长度的数字字符串,并且应用了各种操作。由于您不想使用它,您至少应该对运算符(+, - ,/,*,=)上的字符串进行标记,并修剪空格。 StringTokenizer会在这里提供帮助。

使用拆分可能会有问题,除非您可以保证数字和运算符之间会有空格。您将需要使用其他标记器递归处理字符串,或者在每个标记处进行拆分并相应地处理。