Search code examples
javafile-iotreemap

Creating & Writing to a File


This program prompts the user to enter the name of the file to be written to for a contact list. They enter info such as name, phone and email and it prints to screen and should write to the file. The inputted info prints to the screen and the file gets created, but noting is written to it. What am I missing?

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

public class ContactInfo
{
    public static void main(String[] args) throws IOException 
    {
    try
    {
        int Command = 0;
        String FileName = "";
        Scanner input = new Scanner(System.in); 
        Scanner input2 = new Scanner(System.in);       

        System.out.print("Enter the name of the file for the contact list: ");

        FileName = input.nextLine();
        System.out.println();

        File OutPutFile = new File(FileName);


        PrintWriter ContactList = new PrintWriter(OutPutFile);

        ContactList.format("%15s%12s%20s\n", "Name", "Phone Number", "Email Address" );
        ContactList.format("%15s%12s%20s\n", "------------", "------------", "---------------------" );

        String First_Name = "";
        String Last_Name = "";
        String Remove_Last_Name = "";
        String Phone_Number = "";
        String Email_Address = "";

        //Information InformationObject = new Information(String,String,String,String);
        TreeMap<String, Information> Entries = new TreeMap<String, Information>();



        do
        {
            System.out.println();
            PrintMenu();
            System.out.println();            
            System.out.print("Enter a command ");
            Command = input2.nextInt();
            System.out.println();

            if (Command == 1)
            {

                    System.out.print("Enter the person's first name: ");
                    First_Name = input.nextLine();
                    System.out.println();

                    System.out.print("Enter the person's last name: ");
                    Last_Name = input.nextLine();
                    System.out.println();

                    System.out.print("Enter the person's phone number: ");
                    Phone_Number = input.nextLine();
                    System.out.println();

                    System.out.print("Enter the person's email address: ");
                    Email_Address = input.nextLine();
                    System.out.println();

                    Information InformationObject = new Information(First_Name, Last_Name, Phone_Number, Email_Address);

                    Entries.put(InformationObject.getLastName(), InformationObject );                


            }

            if (Command == 2)
            {
                System.out.print("Enter the last name of the person whom you want to delete from the list: ");
                Remove_Last_Name = input.nextLine();
                System.out.println();      

                Entries.remove(Remove_Last_Name);                  
            }   

            if (Command == 3)
            {
                for (Map.Entry Document : Entries.entrySet() )
                {
                    Information IO = Entries.get( Document.getKey());
                    System.out.println( IO.getLastName() + ", " + IO.getFirstName() + "   " + IO.getPhoneNumber() + "   " + IO.getEmail() );
                    ContactList.format("%15s%12s%20s\n", IO.getFirstName() + IO.getLastName(), IO.getPhoneNumber(), IO.getEmail() );
                }          



            }  






        }
        while(Command != 4);



    }
    catch(Exception e)
    {
        System.out.println("Invalid input");
    }







    }

    public static void PrintMenu()
    {
        System.out.println();
        System.out.println("Add a contact:         <1>");
        System.out.println("Delete a contact:      <2>");
        System.out.println("Display contact list:  <3>");
        System.out.println("Exit:                  <4>");
        System.out.println();
    }



}






class Information
{

    private String FirstName;
    private String LastName;
    private String PhoneNumber;
    private String Email;

    public Information(String FirstName, String LastName, String PhoneNumber, String Email)
    {
        this.FirstName = FirstName;
        this.LastName = LastName;
        this.PhoneNumber = PhoneNumber;
        this.Email = Email;

    } 

    public void setFirstName(String FirstName)
    {
        this.FirstName = FirstName;
    }

    public void setLastName(String LastName)
    {
        this.LastName = LastName;
    }

    public void setPhoneNumber(String PhoneNumber)
    {
        this.PhoneNumber = PhoneNumber;
    }

    public void setEmail(String Email)
    {
        this.Email = Email;
    }

    public String getFirstName()
    {
        return FirstName;
    }

    public String getLastName()
    {
        return LastName;
    }

    public String getPhoneNumber()
    {
        return PhoneNumber;
    }

    public String getEmail()
    {
        return Email;
    }

    public String PrintInformation()
    {
        return LastName + ", " + FirstName + "           " + PhoneNumber + "           " + Email;
    }











}

Solution

  • You forget to close the PrintWriter at the end.

    Use finally block to close the object.

    ContactList.close();