Java 尝试从文本文件中读取公式,存储公式行,计算公式,并将答案写入新文件

Java 尝试从文本文件中读取公式,存储公式行,计算公式,并将答案写入新文件,java,arrays,if-statement,Java,Arrays,If Statement,您好,我在以下代码中遇到问题: 它从具有以下内容的文本文件中读取: 4 * 5 3 / 4 3 - 1 2 + 3 我特别关注声明ArrayList的注释,它用于存储字符串行中的标记化公式,并确定运算符和计算结果的值 import java.io.*; import java.util.*; public class ReadFileLineByLine { public static void main(String[] args) throws FileNotFoundException

您好,我在以下代码中遇到问题: 它从具有以下内容的文本文件中读取:

4 * 5
3 / 4
3 - 1
2 + 3
我特别关注声明ArrayList的注释,它用于存储字符串行中的标记化公式,并确定运算符和计算结果的值

import java.io.*;
import java.util.*;
public class ReadFileLineByLine {
 public static void main(String[] args) throws FileNotFoundException {
 String line;
 Scanner input = null;
 PrintWriter output = null;

 try {

 //open file for reading the calculated formulas "formulas.txt"
 input = new Scanner(new File("C:\\formulas.txt"));
 //open file for storing the calculated formulas "results.txt"
 output = new PrintWriter(new File("C:\\results.txt"));

 // read one line at a time 
 while( input.hasNextLine()) {
 line = input.nextLine();

 System.out.println("read <" + line + ">"); // Display message to commandline
 // Declare ArrayList of for storing tokenized formula from String line

 double result = 0; // The variable to store result of the operation
 // Determine the operator and calculate value of the result
 System.out.println(formula.get(0) + ' ' + formula.get(1) + ' ' +
 formula.get(2) + " = " + result); // Display result to command line
 // Write result to file
 output.println("Print result of " + line + " to Results.txt");
 }
 // Need to close input and output files

 input.close();
 output.close();

 }
 catch (FileNotFoundException e) {
 // Display meaningful error message
  System.out.println("File Not Found: " + e.getMessage());
 }
 }
}
import java.io.*;
导入java.util.*;
公共类ReadFileLineByLine{
公共静态void main(字符串[]args)引发FileNotFoundException{
弦线;
扫描仪输入=空;
PrintWriter输出=null;
试一试{
//打开文件读取计算公式“formulas.txt”
输入=新扫描仪(新文件(“C:\\formulas.txt”);
//打开用于存储计算公式的文件“results.txt”
输出=新的PrintWriter(新文件(“C:\\results.txt”);
//一次读一行
while(input.hasNextLine()){
line=input.nextLine();
System.out.println(“读取”);//向命令行显示消息
//声明的ArrayList,用于存储字符串行中的标记化公式
double result=0;//存储操作结果的变量
//确定运算符并计算结果值
System.out.println(formula.get(0)+''+formula.get(1)+''+
formula.get(2)+“=”+结果);//将结果显示到命令行
//将结果写入文件
output.println(“将“+line+”的结果打印到Results.txt”);
}
//需要关闭输入和输出文件
input.close();
output.close();
}
catch(filenotfounde异常){
//显示有意义的错误消息
System.out.println(“未找到文件:+e.getMessage());
}
}
}

如果有人能想出决定这些评论的代码,我将不胜感激

如果您打算逐行计算表达式,那么我们可以使用

您可以从以下网站获得详细信息:

我修改你的样品如下。请看一看

public static void main(String[] args) {
    String line;
    Scanner input = null;
    PrintWriter output = null;
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("js");
    try {

        // open file for reading the calculated formulas "formulas.txt"
        input = new Scanner(
                new File("/Users/xxx/Downloads/formulas.txt"));
        // open file for storing the calculated formulas "results.txt"
        output = new PrintWriter(new File(
                "/Users/xxx/Downloads/results.txt"));

        // read one line at a time
        while (input.hasNextLine()) {
            line = input.nextLine();
            Object result = null;
            try {
                result = engine.eval(line);
            } catch (ScriptException e) {
                e.printStackTrace();
            }
            // Write result to file
            output.println(line + " = " + result);
        }
        // Need to close input and output files

        input.close();
        output.close();

    } catch (FileNotFoundException e) {
        // Display meaningful error message
        System.out.println("File Not Found: " + e.getMessage());
    }
}
结果如下

4 * 5 = 20
3 / 4 = 0.75
3 - 1 = 2
2 + 3 = 5

这段代码有点凌乱,它没有避免空格,但它就是这样

import java.io.*;
import java.util.*;
public class ReadFromFile {
    public static void main(String[] args) throws FileNotFoundException {
        String line;
        Scanner input = null;
        PrintWriter output = null;
        try
        {
            //open file for reading the calculated formulas "formulas.txt"
            input = new Scanner(new File("C:\\formulas.txt"));
            //open file for storing the calculated formulas "results.txt"
            output = new PrintWriter(new File("C:\\results.txt"));

            // read one line at a time 
            while( input.hasNextLine())
            {
                line = input.nextLine();
                System.out.println("read <" + line + ">"); // Display message to commandline
                //toString("4 * 5".split("\\s+")
                // Declare ArrayList of for storing tokenized formula from String line
                ArrayList<String> formula = new ArrayList<String>();
                for (int i = 0; i < line.length(); i++)
                {
                    formula.add(String.valueOf(line.charAt(i)));
                }
                double result = 0; // The variable to store result of the operation
                // Determine the operator and calculate value of the result
                int firstNum = Integer.parseInt(formula.get(0));
                int secondNum = Integer.parseInt(formula.get(4));
                char operator = formula.get(2).charAt(0);
                result = (operator == '+' ? firstNum + secondNum
                        : operator == '-' ? firstNum - secondNum
                                : operator == '*' ? firstNum * secondNum
                                        : operator == '/' ? firstNum / secondNum : 0);
                System.out.println(formula.get(0) + ' ' + formula.get(2) + ' ' +
                        formula.get(4) + " = " + result); // Display result to command line
                // Write result to file
                output.println("Print result of " + line + " to Results.txt");
            }
            // Need to close input and output files
            input.close();
            output.close();
        }
        catch (FileNotFoundException e)
        {
            // Display meaningful error message
            System.out.println("File Not Found: " + e.getMessage());
        }
    }
}
import java.io.*;
导入java.util.*;
公共类ReadFromFile{
公共静态void main(字符串[]args)引发FileNotFoundException{
弦线;
扫描仪输入=空;
PrintWriter输出=null;
尝试
{
//打开文件读取计算公式“formulas.txt”
输入=新扫描仪(新文件(“C:\\formulas.txt”);
//打开用于存储计算公式的文件“results.txt”
输出=新的PrintWriter(新文件(“C:\\results.txt”);
//一次读一行
while(input.hasNextLine())
{
line=input.nextLine();
System.out.println(“读取”);//向命令行显示消息
//toString(“4*5.split(\\s+”)
//声明的ArrayList,用于存储字符串行中的标记化公式
ArrayList公式=新的ArrayList();
对于(int i=0;i
这里有一个提示:
System.out.println(Arrays.toString(“4*5.split”(\\s+)))