2015-09-26 94 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 - –

相關問題