gpt4 book ai didi

java - 在 Java 中读取输入文件并写入输出文件

转载 作者:行者123 更新时间:2023-12-01 11:07:27 28 4
gpt4 key购买 nike

我目前正在上 Java 类(class)的最后一周,我们的最终项目要求我们让程序从输入 CSV 文件中的单个单元格读取数字和运算符(以逗号分隔),让程序执行数学(从我选择的任何数字开始,然后让程序将结果写入输出 CSV 文件中。我有代码到转换错误,但我确信这是我最不用担心的。我对 Java 的理解很初级,我的类(class)几乎不及格。我只是认为我没有编程的头脑,我已经向教授表达了这一点。所以希望我能在这个最终项目上做得足够好,以提高我的成绩。不用了也就是说,我将立即放弃这个学位计划。

-迈克

这就是教授希望输出的样子:

添加2个总计2

添加6个,总共8个

总共减去9-1

乘以 10 总计 -10

元素数量 = 4,总数 = -10,平均值 = -2.5

错误如下:csvRead2.java:37:错误:类型不兼容:int 无法转换为 String number[i] = (Integer.parseInt(value[0]));//从字符串更改为整数。 ^

import java.io.*;

public class csvRead2 {
public static void main(String args[]) {

String operator[];
String number[];
String total;
int i;

// The name of the file to open.
String inputFile = "mathInput.csv";

// This will reference one line at a time
String line = null;

try { // start monitoring code for Exceptions
// FileReader reads text files in the default encoding.
FileReader read = new FileReader("mathInput.csv");
// Always wrap FileReader in BufferedReader.
BufferedReader buffRead = new BufferedReader(read);

// Assume default encoding.
FileWriter write = new FileWriter("mathOuput.csv", true); // true for append
// Always wrap FileWriter in BufferedWriter.
BufferedWriter buffWrite = new BufferedWriter(write);

// The name of the file to open.
String outputFile = "mathOutput.csv";

while ((line = buffRead.readLine()) != null) {
String[] value = line.split(",");
operator[i] = value[1];
number[i] = (Integer.parseInt(value[0])); // Change from a String to an integer.
// Determine the operator and do the math operation and write to the output file.
if (operator[i].equals("+")) { // if statement for addition operator
total = total + number[i];
buffWrite.write("Add " + number[i] + " total " + total);
buffWrite.newLine();
if (operator[i].equals("-")) { // if statement for subtraction operator
total = total + number[i];
buffWrite.write("Subtract " + number[i] + " total " + total);
buffWrite.newLine();
if (operator[i].equals("*")) { // if statement for multiplication operator
total = total + number[i];
buffWrite.write("Multiply " + number[i] + " total " + total);
buffWrite.newLine();
if (operator[i].equals("/")) { // if statement for division operator
total = total + number[i];
buffWrite.write("Divide " + number[i] + " total " + total);
buffWrite.newLine();
if (operator[i].equals("=")) { // if statement for equals operator
buffWrite.newLine();
}
}
}
}
}
}
// closing BufferedReader and BufferedWriter
buffRead.close();
buffWrite.close();
}
catch(FileNotFoundException ex) { // will catch if file is not found
System.out.println( "Unable to open file '" + inputFile + "'");
}
catch(IOException ex) // catches read and write errors
{
ex.printStackTrace(); // will print read or write error
}
}
}

最佳答案

  1. String number[] 应为 int number[]。如果要对整数进行操作,请将各个变量的数据类型更改为 int。字符串不能用于数字相加。

  2. 即使修复了上述异常,您也不会刷新写入操作。将数据写入文件需要 buffWrite.flush() 。在 bufWrite 上调用 close() 之前先调用 flush()

编辑:存在许多逻辑错误,并且已得到解决。

import java.io.*;

public class CSVRead2 {
public static void main(String args[]) {

String operator[] = new String[1];
int number[] = new int[1];
int total = 0;
int i=0;

// The name of the file to open.
String inputFile = "mathInput.csv";

// This will reference one line at a time
String line = null;

try { // start monitoring code for Exceptions
// FileReader reads text files in the default encoding.
FileReader read = new FileReader("mathInput.csv");
// Always wrap FileReader in BufferedReader.
BufferedReader buffRead = new BufferedReader(read);

// Assume default encoding.
FileWriter write = new FileWriter("mathOuput.csv", true); // true for append
// Always wrap FileWriter in BufferedWriter.
BufferedWriter buffWrite = new BufferedWriter(write);

// The name of the file to open.
String outputFile = "mathOutput.csv";

while ((line = buffRead.readLine()) != null) {
String[] value = line.split(",");
operator[i] = value[1];
number[i] = (Integer.parseInt(value[0])); // Change from a String to an integer.
// Determine the operator and do the math operation and write to the output file.
if (operator[i].equals("+")) { // if statement for addition operator
total = total + number[i];
buffWrite.write("Add " + number[i] + " total " + total);
buffWrite.newLine();
}else if (operator[i].equals("-")) { // if statement for subtraction operator
total = total - number[i];
buffWrite.write("Subtract " + number[i] + " total " + total);
buffWrite.newLine();
}
else if (operator[i].equals("*")) { // if statement for multiplication operator
total = total + number[i];
buffWrite.write("Multiply " + number[i] + " total " + total);
buffWrite.newLine();
}
else if (operator[i].equals("/")) { // if statement for division operator
total = total + number[i];
buffWrite.write("Divide " + number[i] + " total " + total);
buffWrite.newLine();
}
else if (operator[i].equals("=")) { // if statement for equals operator
buffWrite.newLine();
}

}
buffWrite.flush();
// closing BufferedReader and BufferedWriter
buffRead.close();
buffWrite.close();
}
catch(FileNotFoundException ex) { // will catch if file is not found
System.out.println( "Unable to open file '" + inputFile + "'");
}
catch(IOException ex) // catches read and write errors
{
ex.printStackTrace(); // will print read or write error
}
}
}

编辑2:

mathinput.csv(文件中没有空行)

2,+

3,+

9,-

数学输出.csv

添加2个共2个

添加3个,总共5个

总共减去9-4

关于java - 在 Java 中读取输入文件并写入输出文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32799141/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com