Results 1 to 3 of 3
  1. #1
    rugger06 is offline Member
    Join Date
    Apr 2011
    Location
    Florida
    Posts
    6
    Rep Power
    0

    Question 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!

  2. #2
    victor8247 is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    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.

  3. #3
    rugger06 is offline Member
    Join Date
    Apr 2011
    Location
    Florida
    Posts
    6
    Rep Power
    0

    Default

    Thanks Victor,

    Now I get this output in my console: 111-111-1111??????????????????????????????????????

    The numbers represent the user input for the friends phone number and the question marks seem to keep going until I terminate the console.

Similar Threads

  1. Input/ output file
    By 3MAD in forum New To Java
    Replies: 1
    Last Post: 04-15-2011, 01:59 PM
  2. Confused in Exception Handling Assignment!!
    By rugger06 in forum New To Java
    Replies: 5
    Last Post: 04-10-2011, 03:00 PM
  3. File input output
    By edcaru in forum New To Java
    Replies: 5
    Last Post: 12-19-2010, 06:52 PM
  4. Replies: 8
    Last Post: 05-12-2010, 08:19 PM
  5. [SOLVED] Confused with output sequence
    By Niveditha in forum New To Java
    Replies: 5
    Last Post: 07-08-2008, 05:28 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •