1

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

1
  • If i want to sort it by insertion and then perform binary search can it be accomplished (on ArrayList)? Commented Apr 6, 2014 at 19:09

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks bro. I appriciate that.
1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.