将字符串解析为整数并将其存储在数组中

MNM*_*MNM 2 java arrays string split

好的,所以我有点难过如何正确地做这件事.我有一些方法,现在我腰部深.所以我想做的就是解析每行上带有一堆二号的文本文件,就像这样

1 10
2 12 
3 13 
Run Code Online (Sandbox Code Playgroud)

等等....

每个人被一个空间分开.我已经得到了这么远,我甚至得到它为对象中的变量分配正确的数字.我的事情是它继续覆盖数组,并不会用文本文件中的数据填充其余部分.我认为如果打印出阵列,它应该打印出文本文件.感谢您的帮助

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Bri*_*new 5

你不需要这样做吗?

int g = 0;
Run Code Online (Sandbox Code Playgroud)

你的循环之外?否则,您将不断重写数组中的初始值,而不是提升值(填充数组的其余部分).

为了使这更简单,我将填充一个ArrayList<Process>或其他类似的集合而不是固定长度的数组.