affanBajwa
affanBajwa

Reputation: 168

java file int array read

I want to get an array of integers from the file .But when i get an array unwanted zeros are in the array as the size is 10 and there are only 5 integers in file(18,12,14,15,16). How to remove those zeros. Code is:

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;



public class TxtFile {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    File inFile=new File("H:\\Documents\\JavaEclipseWorkPlace\\ReadTextFile\\src\\txt.txt");
    Scanner in=null;
    int []contents = new int[10];
    int i=0;
    try {
        in=new Scanner(inFile);

        while(in.hasNextInt()){
             contents[i++]=in.nextInt();
        }
        System.out.println(Arrays.toString(contents));
    }
    catch(IOException e){
        e.printStackTrace();
    }
    finally{
        in.close();
    }

}

}

Output is: [18, 12, 14, 15, 16, 0, 0, 0, 0, 0].

Upvotes: 1

Views: 338

Answers (3)

NU113R
NU113R

Reputation: 101

You can use a dynamic vector for this

import java.io.File;
import java.io.IOException;
import java.util.*;
class St{
 public static void main(String args[]){
 File inFile=new File("H:\\Documents\\JavaEclipseWorkPlace\\ReadTextFile\\src\\txt.txt");
Scanner in=null;
Vector<Integer> arr=new Vector<Integer>(5,2); //5 is initial size of vector, 2 is increment in size if new elements are to be added
try {
    in=new Scanner(inFile);

    while(in.hasNextInt()){
         arr.addElement(in.nextInt());
    }
arr.trimToSize(); // This will make the vector of exact size
    System.out.println(arr.toString());
}
catch(IOException e){
    e.printStackTrace();
}
finally{
    in.close();
}
}
}

Upvotes: 0

Jason C
Jason C

Reputation: 40335

This is because you allocate an array of size 10, and the values are initialized to 0 by default. Then you read the 5 values from the file, and this only overwrites the first 5 values in the array, the untouched 0's are still there.

You have a few options:

You could count the number of values you read from the file, then resize the array to match, e.g.:

while(in.hasNextInt()){
    contents[i++]=in.nextInt();
}

// 'i' now contains the number read from the file:
contents = Arrays.copyOf(contents, i);
// contents now only contains 'i' items.
System.out.println(Arrays.toString(contents));

You could count the number of values you read from the file, then only explicitly print that many values, e.g.:

while(in.hasNextInt()){
    contents[i++]=in.nextInt();
}

// 'i' now contains the number read from the file:
for (int n = 0; n < i; ++ n)
    System.out.println(contents[n]);

You could use a dynamic container like ArrayList<Integer>, and simply add values to it as you read them. Then you can support any number from the file automagically, e.g.:

ArrayList<Integer> contents = new ArrayList<Integer>();

...
while(in.hasNextInt()){
    contents.add(in.nextInt());
}

System.out.println(contents);

I recommend the third option. It is the most flexible and easiest to deal with.

Upvotes: 2

Reimeus
Reimeus

Reputation: 159784

Read the input file into an ArrayList<Integer> then call toArray to return an integer array

Upvotes: 1

Related Questions