import java.util.*;
import java.io.*;
import java.lang.*;class facetrain
{
public static void main(String[] args)
{
/**********************Initialize the network********************/

// Nodes' Values
double [] In = new double[15360]; try 
{
File in = new File("10_1_1.txt");      FileInputStream file = new FileInputStream(in);
    
     int length = (int)in.length();

byte[] length_byte = new byte[length]; file.read(length_byte);

String s = new String(length_byte);

String[] result = s.split("\\s");
      for (i=0; i<result.length; i++)
      {
    
      System.out.println("The In["+ i + "] = " + result[i]);
      }
}
catch (IOException e) 
{     System.out.println("Could not read file."); } }
}我有一个文件:10_1_1.txt, 里面有这样的数据:
37 40 48 49 49 45 41 34 34 34 33 34 33 33 33 33
32 31 31 31 30 30 29 29 28 27 28 27 27 26 26 26
25 25 24 24 24 23 23 22 22 21 21 20 20 19 19 19
18 19 20 21 25 25 24 22 16 17 15 22 21 20 21 20
20 20 19 19 19 16 20 21 23 23 23 23 23 23 23 24
24 24 25 24 24 25 26 25 25 26 26 25 26 26 26 26
27 27 27 28 28 29 29 29 29 30 29 29 29 29 29 29我的java程序要做的事情主要是把这些数据提取出来,然后存在 double 型数组 In[] 里面,但是用了s.split("\\s")这个之后,得到的结果中会有一些空数据,像这样,33 33   32 31,我猜是由于.txt文件中有“回车”的缘故。
问题1:怎么样把这些空数据去掉?
问题2:怎么样把用s.split("\\s")提取出来的数据存在 double 型数组 In[] 里面, 程序老说:incompatible type,好像是数据类型不对。
各位大侠帮帮忙了,在这里多谢多谢!

解决方案 »

  1.   

    1、
    .....
    String s = new String(length_byte);
    // 加入下行语句
    s = s.replaceAll("\n", "");
    String[] result = s.split("\\s");
    .....2、
    String[] result = s.split("\\s");
                double[] doubles = new double[result.length];
                for (int i = 0; i < result.length; i++) {
                    System.out.println("The In[" + i + "] = " + result[i]);
                    doubles[i] = Double.parseDouble(result[i]);
                    System.out.println("The doubles[" + i + "] = " + doubles[i]);
                }
      

  2.   

    谢谢,emin_lee(),问题都解决了。