// CS 0401 Fall 2010 // Lab 10 // You must modify this file so that it works as described in the lab handout. import java.util.*; import java.io.*; import java.lang.*; public class lab10 { public static void main(String [] args) throws IOException { Scanner inScan, fScan = null; int [] A = new int[5]; inScan = new Scanner(System.in); String fName; do { System.out.println("Please enter the file to read from: "); fName = inScan.nextLine(); } while (!fName.toString().equals("lab10out.txt")); fScan = new Scanner(new File(fName)); String nextItem = " "; int nextInt = 0; int i = 0; while (fScan.hasNextLine()) { try { nextItem = fScan.nextLine(); // reads all lines as strings nextInt = Integer.parseInt(nextItem); // then reads all lines as ints A[i] = nextInt; // all of the numbers are stored in i++; // processes through array until final line is reached if (i>A.length) // if incremented more than array size { int [] temp = new int[A.length*2]; // create new array with the size of array A * 2 ( doubled) == A = 10 int j; for(j=0; j<A.length; j++) { temp[j] = A[j]; // put all of the values of old array into new array Temp } temp[j] = nextInt; // all the new numbers are stored in nextInt soo nextInt = 10 i++; A = new int[temp.length]; // new Array size now array size A is 10 } }catch (NumberFormatException e1) { System.out.println(nextItem + " is not an integer -- Ignored"); }catch (ArrayIndexOutOfBoundsException e2) { System.out.println("Resizing array from " + A.length + "to" + (A.length*2) + "."); } } System.out.println("Here are your " + i + " items:"); for (int j = 0; j < i; j++) { System.out.println(A[j] + " "); } } }
Heres my output:bogus is not an integer -- Ignored
weasel is not an integer -- Ignored
Resizing array from 5to10.
Resizing array from 5to10.
3.5 is not an integer -- Ignored
Resizing array from 5to10.
Resizing array from 5to10.
Resizing array from 5to10.
Resizing array from 5to10.
Resizing array from 5to10.
Resizing array from 5to10.
Here are your 5 items:
100
200
300
150
400
Needed output
bogus is not an integer -- ignored
weasel is not an integer -- ignored
Resizing array from 5 to 10
3.5 is not an integer -- ignored
Resizing array from 10 to 20
Here are your 13 items:
100
200
300
150
400
600
250
800
50
500
450
600
550