import java.io.*; import java.util.Scanner; public class ArrayDoubling { public static void main(String[] args) throws IOException { File inpFile = new File( "ArrayDoubling.inp" ); Scanner inp = new Scanner ( inpFile ); double[] a = new double[1]; double x; int n; // Number of items n = 0; while ( inp.hasNextDouble() ) { x = inp.nextDouble(); System.out.println("Data item " + n + ": " + x); if ( n == a.length ) { /* ========================================== Array is full... increase its size first ========================================== */ System.out.println(" Array is full... increase size to: " + 2*a.length ); double[] h; // help variable to make a bigger array h = new double[ 2*a.length ]; // make aray twice as big for ( int i = 0; i < a.length; i++ ) h[i] = a[i]; // copy old values a = h; // make "a" point to new array } /* ========================================== Here, the array will surely have space ========================================== */ a[n] = x; // Put number read in a[n] n++; // Go to next array position System.out.println(" stored in a[" + n + "]"); } /* ========================================== Done, print values read ========================================== */ for ( int i = 0; i < a.length; i++ ) System.out.println( a[i] ); } }