Java

Moderators: zibadian
Number of threads: 7818
Number of posts: 18218

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Simple enough....??? Posted by jeffsu on 4 Apr 2006 at 3:24 PM
This message was edited by jeffsu at 2006-4-4 15:29:38

This message was edited by jeffsu at 2006-4-4 15:29:31

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'

}
}







 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.