[英]Trying to read multiple lines from a one text file then print out results to new text file java
[英]Trying to read a formula from a text file, store the lines of formulas, calculate the formulas, and write the answers to a new file
您好,我遇到了以下代碼的問題:它從具有以下內容的文本文件中讀取:
4 * 5
3 / 4
3 - 1
2 + 3
我特別堅持使用聲明 ArrayList of 用於存儲字符串行中的標記化公式和確定運算符並計算結果值的評論。
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());
}
}
}
如果有人能提出決定這些評論的代碼,我將不勝感激!
如果您打算逐行評估表達式,那么我們可以使用ScriptEngine
您可以從這些帖子中獲得詳細信息
我修改了你的樣本如下。 請看一看。
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());
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.