2014-10-17 45 views
0

我想讀取文本文件的線條,走數字的平均值。我的計劃是先讀取文本文件中的所有數據。然後將字符串分成一個字符串數組,然後將每個索引解析爲一個int數組。這是我的代碼,直到閱讀文檔。如何在一個文本文件中讀取值,然後進行再分析它們以整數的JAVA

我的文本文檔:

3,7,24,66,

10,50,20,40,

100,20,69,911,

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 

public class Testing 
{ 
    public static void main(String[] args) throws IOException 
    { 
     try 
     { 
      String path; 
      path = "TestNumbers.txt"; 
      File f = new File(path); 
      FileReader re = new FileReader(f); 
      BufferedReader in = new BufferedReader(re); 
      String line = ""; 
      String store = ""; 
      while((line = in.readLine()) != null) 
      { 
       store = store + line; 
      } 
      System.out.println(store); 
     } 
     catch (FileNotFoundException e) 
     { 
      e.printStackTrace(); 
     } 
    } 
} 

輸出:3,7,24,66,10,50,20,40,100,20,69,911,

在一個完美的世界中,我希望值被eit她「,」或「」。

我試着修改store = store + line + " ";,但是這樣做失敗了,因爲即使readLine()在空行上,它也會迭代空間。

我可以遍歷數組來解析int並取平均值,但是設置這個字符串被分割會困擾我。我試過String.replace()String.trim(),和另外一個失敗的我。這不是家庭作業,我在高中AP CS,這是我自己的獨立研究。

感謝大家的幫助,你都表現出很多方法可以做到這一點。結束後,使用.replace,並將「」變成「」。然後通過逗號分開。儘管如此,我還是想嘗試一下這個正則表達式。再次感謝大家。

+0

調查Sting.split()。它有你在找什麼。 – user3473949 2014-10-17 02:44:07

+0

@ user3473949 OP已經提過關於 – DnR 2014-10-17 02:44:43

+0

'if(line.length()> 0){store = store + line;}' – 2014-10-17 02:47:02

回答

0

Scary Wombatmentioned如何處理空行。

你可以做一個.replace掉所有的「」字符爲「」字符。如果你期望雙方都喜歡「,」在某些情況下,你甚至可以先把它們換成「,」。這些基本上都是技巧。

你可以看看String.split(正則表達式),因爲正則表達式可以是像「[,] +」這樣的模式,可以將任意數量的空格和/或數字之間的逗號作爲分隔符。

這裏有一個快速演示我測試(跳過文件輸入,因爲你似乎有想通了):

public class tmp { 

    public static void main(String[] args) { 
     String input = "1,2, 3, 4\n5, 6, 7\n\n,8"; //what we would read from file 
     input = input.replace("\n"," "); //simulate removing newlines, ignore this 
     String[] items = input.split("[, ]+"); //regex split 
     for(String one : items) { 
      System.out.println(one); 
     } 
    } 

} 
0

像這樣的事情?

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 

public class Testing 
{ 
    public static void main(String[] args) throws IOException 
    { 
     try 
     { 
      //open a file 
      String path; 
      path = "TestNumbers.txt"; 
      File f = new File(path); 
      FileReader re = new FileReader(f); 
      BufferedReader in = new BufferedReader(re); 

      String line = ""; //reads a line 
      String store = ""; //combines all the lines 

      while((line = in.readLine()) != null) //for each line 
      { 
       if(line.length() > 0){ //only add to store if the line contains something 
        store = store + line + " "; 
       } 
      } 

      //create the string array 
      String[] finalString = store.split(", "); 
      //and the integer array to hold the numbers 
      int [] intArray = new int [finalString.length]; 
      //parse the string array and put each index to the integer array 
      for (int i = 0; i < finalString.length; i++){ 
       intArray[i] = Integer.parseInt(finalString[i]); 
      } 

      //simply calculating the average 
      //summing up 
      int sum = 0; 
      for (int i = 0; i < intArray.length; i++){ 
       sum = sum + intArray[i]; 
      } 
      //printing the average 
      System.out.println("Average: " + sum/intArray.length); 
     } 
     catch (FileNotFoundException e) 
     { 
      e.printStackTrace(); 
     } 
    } 
} 
2

另外兩個方案可以是已經你需要什麼,但是這裏是一個更地道的做法,處理邊界情況正確

{ 
    String path = "TestNumbers.txt"; 
    try (BufferedReader reader = new BufferedReader(new FileReader(path))) { 
     StringBuilder builder = new StringBuilder(); 
     String line; 
     while ((line = reader.readLine()) != null) 
      builder.append(line); 
     String[] split = builder.toString().split("\\D+"); 
     int[] numbers = new int[split.length]; 
     for (int i = 0; i < split.length; i++) 
      numbers[i] = Integer.parseInt(split[i]); 
     // 'numbers' now stores every digit segment there is in your string. 
    } catch(IOException e) { 
     e.printStackTrace(); 
    } 
} 

亮點:

  • 聲明BufferedReader作爲一個嘗試,有資源範圍Closeable
  • 請不要將String s連接成循環,而應使用StringBuilder
  • 拆分爲\D+刪除所有非數字,取而代之的是在最終數組中獲取數字段作爲元素。
相關問題