2014-10-17 49 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+删除所有非数字,取而代之的是在最终数组中获取数字段作为元素。
相关问题