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].