【问题标题】:Parsing a string to Integers and storing them in an array将字符串解析为整数并将它们存储在数组中
【发布时间】:2012-09-29 20:12:49
【问题描述】:

我有点不知道如何正确地做这件事。我有一些方法进入它,现在我已经齐腰深了。所以我想要做的是解析一个文本文件,每行有一堆数字 2 像这样。

1 10
2 12 
3 13 

等等……

每一个都被一个空格分隔。我已经做到了这一点,我什至可以将正确的数字分配给对象中的变量。我的事情是它不断覆盖数组,并且不会用文本文件中的数据填充数组的其余部分。我认为如果我打印出数组,它应该基本上打印出文本文件。

public static void main(String[] args) {
      
    Process [] pArray;
    //just need to have 10 Process
    pArray = new Process [10];
    
    //delimiter to parse string
    String delimiter = " ";
    String[] tokens;
    tokens = new String [10];
    
    /*
     * save this for input handeling
    Scanner input = new Scanner( System.in );
    System.out.println("Enter the Text file for data set");
    String fileDestination = input.next();
    * */
    
    //get data from the file
    File file = new File("C:/Users/Kenshin/Desktop/TestData.txt");
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    DataInputStream dis = null;
    try {
  
        fis = new FileInputStream(file);     
        // Here BufferedInputStream is added for fast reading.     
        bis = new BufferedInputStream(fis);   
        dis = new DataInputStream(bis);    
        // dis.available() returns 0 if the file does not have more lines.    
        while (dis.available() != 0) {   
            // this statement reads the line from the file and print it to       
            // the console.        
            int g = 0;
            //System.out.println(dis.readLine());
            tokens = dis.readLine().split(delimiter);
            int aInt = Integer.parseInt(tokens[0]);
            int bInt = Integer.parseInt(tokens[1]);
            for( int i = 0; i < tokens.length; i ++)
            {
                
                //int aInt = Integer.parseInt(tokens[i]);
                pArray[g] = new Process(aInt, bInt);
                
            }
          
            g++;
            
        }
  
        // dispose all the resources after using them.     
        fis.close();     
        bis.close();
        dis.close();
    } catch (FileNotFoundException e) {  
        e.printStackTrace();
    } catch (IOException e) {  
        e.printStackTrace();
    }
    
    for(int i = 0; i < pArray.length; i ++)
    {
        System.out.print(pArray[i].arrivalTime + " ");
        System.out.println(pArray[i].burstTime);
    }
}

【问题讨论】:

  • 您不会在循环内增加g
  • 如果你使用Java 7,你可以用List&lt;String&gt; lines = Files.readAllLines(..);去掉可能的行。
  • @Wug 你看错了循环。
  • @DavidGrant:没错,这是少数问题之一。

标签: java arrays string split


【解决方案1】:

首先:您在 while 循环的每次迭代中重新初始化 g;其次:您必须在 for 循环内增加 g 或使用不同的变量作为 pArray 的索引。否则,它会在 for 循环的每次迭代中继续覆盖索引 0 处的值。

int g = 0;
while (dis.available() != 0) {
    // this statement reads the line from the file and print it to
    // the console.

    //System.out.println(dis.readLine());
    tokens = dis.readLine().split(delimiter);
    int aInt = Integer.parseInt(tokens[0]);
    int bInt = Integer.parseInt(tokens[1]);
    for( int i = 0; i < tokens.length; i ++) {
        //int aInt = Integer.parseInt(tokens[i]);
        pArray[g] = new Process(aInt, bInt);
        g++;
    }
}

【讨论】:

  • 在第六次迭代中,pArray[g] 越界了。
【解决方案2】:

你不需要这样做

int g = 0;

你的循环之外?否则,您会不断地重写数组中的初始值,而不是推进该值(填充数组的其余部分)。

为了更简单,我将填充一个ArrayList&lt;Process&gt; 或其他类似的集合,而不是一个固定长度的数组。

【讨论】:

    【解决方案3】:

    有两个问题:第一,你不需要每次都重新初始化g。其次,你的内部 for 循环什么都不做(唯一使用 i 的行被注释掉了)。请尝试以下操作:

    int g = 0;
    while (dis.available() != 0) {   
        tokens = dis.readLine().split(delimiter);
        int aInt = Integer.parseInt(tokens[0]);
        int bInt = Integer.parseInt(tokens[1]);
        pArray[g++] = new Process(aInt, bInt);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多