我目前处于Java类的最后一周,我们的最终项目需要我们让程序读取数字和运算符(用逗号分隔)输入CSV文件中的单个单元格,让程序执行数学运算(从我选择的任意数字开始,然后让程序将结果写入输出CSV文件。我将代码缩减为转换错误,但我是我对Java的理解是最基本的,而且我几乎失败了,我只是觉得我没有编程思想,并且已经向教授表达了,所以希望我能做到这个最后的项目已经足够让我的成绩升级了,不用说,我马上就会避开这个学位计划。-Mike从输入文件中读取数据并写入Java文件中的输出文件
这是想要的东西教授输出看起来像:
添加2共2
添加6共8
减去9总-1
乘10总数-10
元素数量= 4,总数= -10,平均值= -2.0
这是错误: csvRead2.java:37:错误:不兼容的类型:int不能转换为字符串 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
}
}
}
你正试图把一个int中的字符串(数字)数组... –