2015-09-26 119 views
0

我目前处于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 
     } 
    } 
} 
+0

你正试图把一个int中的字符串(数字)数组... –

回答

0

首先,阵列的类型从字符串[] INT []改变。您正在将整数存储到字符串数组中,因此是例外。

0

valueString的数组,number也是String的数组。 现在,您将从value中取出一个字符串,将其解析为一个整数并尝试再次将它放入一个字符串数组中!

您可以将String number[]的类型更改为int number[]

这同样适用于您的变量总数!既然你在那里存放的整数值,你应该改变它的类型来int total

编辑:

此外,我注意到您在链接的,如果块!我想你可能想再想一想这个概念!如果您收到"-",则您的第一个if块(if (operator[i].equals("+")))将导致false,并且该if块中的所有代码都不会执行!

+0

哦,我明白了,我猜我不应该把它们放在那里。只是让随后的if语句正确吗? – Mike

+0

@Mike或者你可以使用'else if(...)'语句。但是,是的,随后的if语句会起作用! –

1
  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, -

mathOutput.csv

添加2共2

添加3总共5

减去9总-4

+0

我能够运行并获得输出。按原样运行程序 –

+0

注意到逻辑错误。感谢帮助!该程序正在编译,但是,我现在正在运行该程序的问题。输入字符串“+”是指我的mathInput文件中的第一个运算符。文件看起来不错,为什么它会给我这个错误?线程“main”中的异常java.lang.NumberFormatException:对于输入字符串:“”+“at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)at java.lang.Integer.parseInt(Integer.java:569 )在java.lang.Integer.parseInt(Integer.java:615)at CSVRead3.main(CSVRead3.java:34) - – Mike

+0

在文件中传递如下代码行1:2 +行2:3 +行3: 9 - –