您好,我遇到了以下代码: 它从具有以下内容的文本文件中读取:
4 * 5
3 / 4
3 - 1
2 + 3
我特别停留在声明声明ArrayList以便存储来自String行的标记化公式并确定运算符并计算结果值的注释上。
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());
}
}
}
如果有人能提出确定这些注释的代码,我将不胜感激!
答案 0 :(得分:0)
如果您打算逐行评估表达式,则可以使用ScriptEngine
您可以从these post
获取详细信息我修改了您的示例,如下所示。请看看。
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
答案 1 :(得分:0)
这段代码有点混乱,它不能避免空格,但是在这里。
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());
}
}
}