Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Simple enough....???

jeffsujeffsu Posts: 1Member
[b][red]This message was edited by jeffsu at 2006-4-4 15:29:38[/red][/b][hr]
[b][red]This message was edited by jeffsu at 2006-4-4 15:29:31[/red][/b][hr]
Okay, working on this file. It needs to input information from a file (phonebook.dat) that has last name, first name, phone number. It needs to be stored in a 2d array so that each bit of info could be individually pulled out. Then it needs to print the whole thing.

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 doens'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


// Print
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'

}
}




Sign In or Register to comment.