This is what phonebook.dat looks like:
Lange Albin 444-2742
Richards Carole 243-5816
Powell Melanie 683-2309
Poshard John 353-2301
Schmidt David 674-7762
Wagoner Rodney 699-9820
Brown Roberta 444-2854
Barnette Amy 822-2273
Davis Darin 354-5611
Gasper Reginald 243-3158
Hohulin Matt 676-2911
Lampkin Kenneth 694-1626
Maxion Patsy 742-2151
Contrary Mary 677-2243
Jordan William 222-5361
Barnes Michael 861-3344
Burnett Julie 651-4442
Ramsey Richard 987-9876
Lead Brenda 443-7665
--------------------------------------------------------------------------
Here is what my code looks like. It always gets hung up on the catch statement... HELP!! It's driving me crazy. The output in the program should look pretty much exactly like the original phonebook.dat file. Should be so simple, but it just doesn't work.. grr!
--------------------------------------------------------------------------
package lab10;
import java.io.*;
import java.util.Scanner;
public class Main {
public Main() {
}
public static void main(String[] args) {
populateArray();
}
public static void populateArray() {
String firstName; // To temp store input First Name
String lastName; // To temp store input Last Name
String phoneNumber; // To temp store input phone number
int numRows = 18;
int numCols = 2;
String[] [] phone = new String [numRows] [numCols];
File tempFile = new File ( "phonebook.dat" );
try {
Scanner scan = new Scanner (tempFile);
int i = 0;
while ( scan.hasNext() ) {
lastName = scan.next();
firstName = scan.next();
phoneNumber = scan.next();
phone[i][0] = lastName;
phone[i][1] = firstName;
phone[i][2] = phoneNumber;
i++;
} // Ends WHILE loop
scan.close();
} // Ends TRY
catch ( Exception exception ) {
System.out.println("An Error Occurred: File Not Found.");
System.exit(1);
} // Ends CATCH
System.out.println("Phone Book: ");
for (int x = 0; x <= numRows; x++) {
for (int y = 0; y < numCols; y++) {
System.out.print( phone[x][y] );
} // Ends INNER 'for'
System.out.println();
}// Ends OUTER 'for'
}
}