Results 1 to 3 of 3
- 04-16-2011, 12:49 PM #1
Member
- Join Date
- Apr 2011
- Location
- Florida
- Posts
- 6
- Rep Power
- 0
Confused about File Input and Output Assignment
This is my last Java programming assignment before my final exam. Unfortunately, neither my textbook not the online module is really helping me figure this assignment out!
The purpose of this assignment is to create a sequential file and then use that file by printing its contents. This assignment requires an application (not an applet) since I am using files.
Assignment Requirements:
1. Create one source file named PhoneList.java. This application will create a file of names and phone numbers.
2. Prompt the user to enter the file name using a dialog box. Dialog box says: "Please enter the file name. \nInclude drive and folder (C:\Temp\file_name)." There are no edits required for this data.
3. Prompt the user to enter the name of your friend using a dialog box. Dialog box says: "Please enter friend's name:". There are no edits required for this data.
4. Prompt the user to enter the telephone number using a dialog box. Dialog box says" "Please enter friend's telephone number (nnn-nnn-nnnn):" There are no edits required for this data.
5. Write the name and telephone number to a file.
6. Prompt the user to determine if he/she has more data to enter. Repeat the above process if the answer is yes. Read the file and print all the information on the console screen if the answer is no and then end the application. Your printed output on the console will look like this: "The Employee name is: John Doe phone extension is : 212-555-1234"
7. All statements that read/write files, must be in try blocks. Each try block block must have at least one catch block. You may have more. You must use at least one finally block. All statements that instantiate I/O objects must be in try blocks. Each try block block must have at least one catch block. You may have more. You may use System.exit(1) to end your application if any of the object instantiations fail or if you have an I/O error.
8. Do not use any class wide variables. That is, all the variables you create in this assignment must be declared within a method.
Here is my code so far:
Java Code:import java.io.*; import javax.swing.*; public class PhoneList { public static void main(String[] args) throws Exception { String friendName; String fileName; String phoneNumber; fileName = getFileName(); friendName = getFriendName(); phoneNumber = getPhoneNumber(); FileOutputStream outputPhoneListFile = new FileOutputStream(fileName); FileInputStream inputPhoneListFile = new FileInputStream(fileName); boolean isYes = true; while (isYes == true) { try { writeToFile(outputPhoneListFile, friendName, phoneNumber); } catch (IOException e) { System.out.println("Unable to create " + fileName +": " + e.getMessage()); } finally { outputPhoneListFile.close(); System.exit(1); } try { displayNamesAndNumbers(inputPhoneListFile, fileName, phoneNumber); } catch (IOException e) { System.out.println("Unable to display information"); } finally { inputPhoneListFile.close(); System.exit(1); } int selection = JOptionPane.showConfirmDialog(null, "Do you want to enter " + "any more names and numbers?", "Input Additional Names", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if (isYes = (selection == JOptionPane.NO_OPTION)); { isYes = false; } if (isYes = (selection == JOptionPane.YES_OPTION)); { } } } private static String getFileName() { return JOptionPane.showInputDialog(null, "Please enter the file name." + "\nInclude drive and folder (C:\\Temp\\file_name)", "File Name", JOptionPane.QUESTION_MESSAGE); } private static String getFriendName() { return JOptionPane.showInputDialog(null, "Please enter friend's name:", "Name", JOptionPane.QUESTION_MESSAGE); } private static String getPhoneNumber() { return JOptionPane.showInputDialog(null, "Please enter friend's " + "telephone number (nnn-nnn-nnnn):", "Phone Number", JOptionPane.QUESTION_MESSAGE); } private static void writeToFile(FileOutputStream outputFile, String friendName, String phoneNumber)throws IOException { int characterCode; for(int characterNumber =0;characterNumber<phoneNumber.length();++characterNumber) { characterCode = (char) phoneNumber.charAt(characterNumber); outputFile.write(characterCode); } } private static void displayNamesAndNumbers(FileInputStream filePhoneList, String fileName, String phoneNumber) throws IOException//constructor takes File objects as arguments { char inputCharacter; System.out.println("The Employee name is: " + fileName + " phone extension is:" +phoneNumber + "\n"); while ((inputCharacter = (char)filePhoneList.read())!='#') { System.out.print (inputCharacter); } System.out.println(); } }
I've tried to write this as best as I can by using examples in my textbook and previous assignments, but I'm certain that there are quite a few logic errors in my code. I just don't know enough to identify and fix them.
When I run the application, the first three dialog boxes execute, the application terminates and I get nothing in my console. :confused:
Does anyone have any recommendations on how to fix my code to make this work? Thank you!
- 04-16-2011, 01:28 PM #2
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
try
{
writeToFile(outputPhoneListFile, friendName, phoneNumber);
}
catch (IOException e)
{
System.out.println("Unable to create " + fileName +": " + e.getMessage());
}
finally
{
outputPhoneListFile.close();
System.exit(1);
}
no matter it throws the IOException, the finally block will be executed all the time. so , you application is exit. you can not get any output in your console.
- 04-17-2011, 02:10 AM #3
Member
- Join Date
- Apr 2011
- Location
- Florida
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
Input/ output file
By 3MAD in forum New To JavaReplies: 1Last Post: 04-15-2011, 12:59 PM -
Confused in Exception Handling Assignment!!
By rugger06 in forum New To JavaReplies: 5Last Post: 04-10-2011, 02:00 PM -
File input output
By edcaru in forum New To JavaReplies: 5Last Post: 12-19-2010, 05:52 PM -
how to change the layout of an input file and write to an output file
By renu in forum New To JavaReplies: 8Last Post: 05-12-2010, 07:19 PM -
[SOLVED] Confused with output sequence
By Niveditha in forum New To JavaReplies: 5Last Post: 07-08-2008, 04:28 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks