MNM
MNM

Reputation: 2743

Parsing a string to Integers and storing them in an array

I'm a bit stumped on how to go about doing this right. I got some ways into it and now I'm waist deep. So what I want to do is parse a text file with a bunch of number two on each line like this.

1 10
2 12 
3 13 

etc....

Each one is split by one space. I have gotten that far I even got it to assign the correct number to the variables in the object. My thing is it keep overwriting the array and won't fill in the rest of it with the data from the text file. I think it should print off the text file basically if I print off the array.

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);
    }
}

Upvotes: 2

Views: 1027

Answers (3)

Martin Green
Martin Green

Reputation: 1054

Firstly: you're reinitilizing g on every iteration of while-loop; secondly: you have to increment g inside the for-loop or use a different variable as an index for pArray. Otherwise it will keep overwritting value at index 0 on each iteration of for-loop.

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++;
    }
}

Upvotes: 0

Brian Agnew
Brian Agnew

Reputation: 272297

Don't you need to do this

int g = 0;

outside your loop ? Otherwise you're continually rewriting your initial value in the array and not advancing the value (populating the rest of the array).

To make this simpler, I would populate a ArrayList<Process> or other similar collection rather than a fixed-length array.

Upvotes: 5

David Grant
David Grant

Reputation: 14223

There are two problems: firstly, you don't need to reinitalize g each time. Secondly, your inner for loop does nothing (the only line using i is commented-out). Try the following:

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);
}

Upvotes: 2

Related Questions