Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Java program error - HELP!


  • Please log in to reply

#1
jeffsu

jeffsu

    New Member

  • Member
  • Pip
  • 1 posts
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 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


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

}
}
  • 0

Advertisements


#2
Thef0rce

Thef0rce

    Member

  • Member
  • PipPipPip
  • 380 posts
try using

catch(FileNotFoundException e)

I think perhaps you need to be more specific than just using the general 'exception'
  • 0

#3
coyne20

coyne20

    Member

  • Member
  • PipPip
  • 35 posts
try running the following:

package examples;

import java.io.*;
import java.util.Scanner;

public class PhoneBook {


public PhoneBook() {
}


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 = 20;
int numCols = 3;

String[] [] phone = new String [numRows] [numCols];

File tempFile = new File ( "C:/jar/phonebook.dat" );
//!!!! Use the absolute directory to locate your file and use the unix convention directory path assignation.

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 ( ArrayIndexOutOfBoundsException exception ) {
exception.printStackTrace();
//System.out.println("An Error Occurred: File Not Found.");
//System.exit(1);
}

catch ( FileNotFoundException exception ) {
exception.printStackTrace();
//System.out.println("An Error Occurred: File Not Found.");
//System.exit(1);
}

catch ( Exception exception ) {
exception.printStackTrace();
//System.out.println("An Error Occurred: File Not Found.");
//System.exit(1);
}

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

}
}
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP