Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Help printing output for Contact Manager assignment

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help printing output for Contact Manager assignment

    Hi, I have an assignment for object-oriented programming class where I have to make a contact list manager. The program gives you the option of creating business or personal contacts. Then you can print the contacts from the menu. I am having trouble getting it to print out. Here is the requirements for that part of the assignment:

    Output Requirements

    1. Display the results to the user on screen in a readable and descriptive format (e.g., System.out) by doing the following:

    a. Display all contacts’ first and last names when the “display contacts” command is selected.

    b. Include a numeric key for each contact that will be used to invoke the method that displays the contact details.

    2. Include a prompt that allows the user to enter the numeric key to display all details of the contact in a readable and descriptive format (e.g., System.out, output to a text file using FileWriter).

    a. Identify contacts by type when displaying the details of a contact: business or personal.

    Here is my code for that part:
    int counter = 0;
                String line = null;
     
                // Location of file to read
                File file = new File("contactlist.csv");
     
                // Sort contacts and print to console
                try {
                    try (Scanner scanner = new Scanner(file)) {
                        Set<String> lines = new TreeSet<>();
                        while (scanner.hasNextLine()) {
                            line = scanner.nextLine();
                            lines.add(line);
                            counter++;
     
                        }
     
                        // Print sorted contacts to console.
                        for (String fileLine : lines) {
                            String outlook = fileLine.substring(0, 1).toUpperCase()
                                    + fileLine.substring(1);
                            System.out.println(outlook);
     
                        }
                    }
     
                } catch (FileNotFoundException e) {
     
                }
                System.out.println("\n" + counter + " contacts in records.");
     
            }
    When I choose the menu option to display the contacts, all it shows is the number of contacts in records. Can anyone help me fix this? I would greatly appreciate any help!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help printing output for Contact Manager assignment

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    Can anyone help me fix this?
    Can you explain what problems you are having? Do you have any specific questions about the program?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help printing output for Contact Manager assignment

    There doesn't appear to be anything wrong with the code you posted that would print the data, but before the data is printed it is being read from a file. Verify that the data is being read from the file and stored correctly. An empty catch block does you absolutely no good and is a bad, lazy practice.

    It's odd that you have to read the data from the file before printing it. Why isn't the data already stored in a data structure or some kind of collection in memory?

  4. #4
    Junior Member
    Join Date
    Dec 2013
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help printing output for Contact Manager assignment

    How would I verify that the data is being read and stored correctly?

    The instructions for the assignment requires it to be stored in a file and displayed from the file.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help printing output for Contact Manager assignment

    Fix the catch block, add print statements that indicate what is being read and displays the contents of the data structure before trying to print it.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help printing output for Contact Manager assignment


  7. #7
    Junior Member
    Join Date
    Dec 2013
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help printing output for Contact Manager assignment

    Found out the csv file is null for each contact, so the problem is with the writer instead of with displaying the output.
    Here is the code for that part:
    contact contact = null;
                boolean add = contacts.add(contact);
     
                try {
     
                    contact = new contact();
                    contact c;
                    c = contact;
     
                    File file = new File("contactlist.csv");
     
                    // If file doesn't exists, then create it.
                    if (!file.exists()) {
                        file.createNewFile();
                    }
     
                    try (PrintWriter output = new PrintWriter(new FileWriter(
                            "contactlist.csv", true))) {
                        output.printf("%s\r\n", c);
                    } catch (Exception e) {
                    }
     
                    System.out.println("Your contact has been saved.");
                }
     
                catch (IOException e) {
        }
            }

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help printing output for Contact Manager assignment

    Please respect Java's naming convention and capitalize class names. Because you didn't do that, this statement is more than suspect:

    contact contact = null;

    And what's the purpose of this shell game:

    contact = new contact();
    contact c;
    c = contact;

    Same as before, if the file is being written with nulls, find out why. What is the object being written to the file by the code you've posted? Add print statements to find out. Then continue working backwards to answer the question, "Why are nulls being written to the file?" When you get to the root of the answer, fix it.

    You might also search on "java saving objects" or "java writing objects to file".

  9. The Following User Says Thank You to GregBrannon For This Useful Post:

    Strong1 (December 26th, 2013)

  10. #9
    Junior Member
    Join Date
    Dec 2013
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help printing output for Contact Manager assignment

    I have worked out most of the problems but am still having trouble figuring out how to do part 2 of the output requirements:

    2. Include a prompt that allows the user to enter the numeric key to display all details of the contact in a readable and descriptive format (e.g., System.out, output to a text file using FileWriter).

    a. Identify contacts by type when displaying the details of a contact: business or personal.

    Here is my code I have now:

    import java.io.*;
    import java.util.*;
     
    public abstract class ContactList {
     
        public static void main(String args[]) throws IOException {
            contact ContactList;
            ContactList = new contact();
            int action = 0;
     
     
            ArrayList<contact> contacts = new ArrayList<>();
            while (action != 6) {
     
                System.out.println("\nWelcome to Contact Manager. "
                        + "What would you like to do? \n");
     
                System.out.println("1. Add a business contact" + "\n"
                        + "2. Add a personal contact" + "\n"
                        + "3. Display contacts" + "\n"
                        + "4. Quit" + "\n");
                Scanner reader = new Scanner(System.in);
                reader.useDelimiter("\n");
                action = reader.nextInt();
     
                if (action <= 0 || action > 4) {
                    System.out.println("Invalid selection. ");
     
                }
     
                switch (action) {
     
                    case 1: {
     
                        BusinessContact myContact = new BusinessContact();
                        System.out.println("\nEnter Contact Last Name:");
                        String lastname = reader.next();
                        if (lastname == null) {
                            System.out.println("\nInvalid entry. ");
                            break;
                        } else {
     
                            myContact.setLastName(lastname.toLowerCase());
     
                        }
                        System.out.println("Enter Contact First Name: ");
                        String firstname = reader.next();
                        myContact.setFirstName(firstname.toLowerCase());
                        System.out.println("Enter Contact Street Address: ");
                        String address = reader.next();
                        myContact.setHouseAddress(address.toLowerCase());
                        System.out.println("Enter Contact City: ");
                        String city = reader.next();
                        myContact.setCity(city.toLowerCase());
                        System.out.println("Enter Contact Zip Code: ");
                        String zip = reader.next();
                        myContact.setZip(zip.toLowerCase());
                        System.out.println("Enter Contact Email: ");
                        String email = reader.next();
                        myContact.setEmail(email.toLowerCase());
                        System.out.println("Enter Contact Phone Number: ");
                        String phone = reader.next();
                        myContact.setPhone(phone.toLowerCase());
                        System.out.println("Enter Contact Job Title: ");
                        String jobtitle = reader.next();
                        myContact.setJobtitle(jobtitle.toLowerCase());
                        System.out.println("Enter Contact Organization: ");
                        String organization = reader.next();
                        myContact.setOrganization(organization.toLowerCase());
                        contacts.add(myContact);
     
                        File file = new File("contactlist.csv");
     
                        // If file doesn't exists, then create it.
                        if (!file.exists()) {
                            file.createNewFile();
                        }
     
                        try (PrintWriter output = new PrintWriter(new FileWriter(
                                "contactlist.csv", true))) {
                            output.printf("%s\r\n", myContact);
                            output.close();
                        } catch (Exception e) {
                            System.out.println("File not found.");
                        }
     
                        System.out.println("Your contact has been saved.");
     
                    }
     
     
     
                    break;
     
     
     
                    case 2: {
     
                        PersonalContact myContact = new PersonalContact();
                        System.out.println("\nEnter Contact Last Name:");
                        String lastname = reader.next();
                        if (lastname == null) {
                            System.out.println("\nInvalid entry. ");
                            break;
                        } else {
                            myContact.setLastName(lastname.toLowerCase());
     
                        }
                        System.out.println("Enter Contact First Name: ");
                        String firstname = reader.next();
                        myContact.setFirstName(firstname.toLowerCase());
                        System.out.println("Enter Contact Street Address: ");
                        String address = reader.next();
                        myContact.setHouseAddress(address.toLowerCase());
                        System.out.println("Enter Contact City: ");
                        String city = reader.next();
                        myContact.setCity(city.toLowerCase());
                        System.out.println("Enter Contact Zip Code: ");
                        String zip = reader.next();
                        myContact.setZip(zip.toLowerCase());
                        System.out.println("Enter Contact Email: ");
                        String email = reader.next();
                        myContact.setEmail(email.toLowerCase());
                        System.out.println("Enter Contact Phone Number: ");
                        String phone = reader.next();
                        myContact.setPhone(phone.toLowerCase());
                        System.out.println("Enter Date of Birth: ");
                        String dateofbirth = reader.next();
                        myContact.setDateofbirth(dateofbirth.toLowerCase());
     
     
                        File file = new File("contactlist.csv");
     
                        // If file doesn't exists, then create it.
                        if (!file.exists()) {
                            file.createNewFile();
                        }
     
                        try (PrintWriter output = new PrintWriter(new FileWriter(
                                "contactlist.csv", true))) {
                            output.printf("%s\r\n", myContact);
                            output.close();
                        } catch (Exception e) {
                            System.out.println("File not found.");
                        }
     
                        System.out.println("Your contact has been saved.");
                    }
     
     
     
                    break;
     
     
                    case 3: {
     
                        int counter = 0;
                        String line = null;
     
                        // Location of file to read
                        File file = new File("contactlist.csv");
     
                        // Sort contacts and print to console
                        try (Scanner scanner = new Scanner(file)) {
                            Set<String> lines = new TreeSet<>();
                            while (scanner.hasNextLine()) {
                                line = scanner.nextLine();
                                lines.add(line);
                                counter++;
                                System.out.println(counter);
                            }
     
                            // Print sorted contacts to console.
     
                            for (String fileLine : lines) {
                                String outlook = fileLine.substring(0, 1).toUpperCase()
                                        + fileLine.substring(1);
                                System.out.println(outlook);
                            }
                        } catch (Exception e) {
                            System.out.println("File not found.");
                            System.out.println("\n" + counter + " contacts in records.");
                        }
                    }
     
                    break;
     
     
                    case 4: {
     
                        System.exit(0);
     
                    }
                }
            }
        }
     
        public static void getContacts(ArrayList<contact> myContact) {
            Scanner keyIn = new Scanner(System.in);
            System.out.println("Contact Names:");
            for (int i = 0; i < myContact.size(); i++) {
     
                System.out.println(i + ") " + myContact.get(i).getfirstName() + " " + myContact.get(i).getLastName());
            }
            System.out.println("Please enter the number corresponding to the contact to see details: ");
            int choice = keyIn.nextInt();
     
            System.out.println(myContact.get(choice).toString());
     
     
        }
    }

    I know it's not the ideal way to do the assignment but my instructor said I was too close to start over, so can anyone tell me where and how to call the method to display the requirements. Thank you!

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help printing output for Contact Manager assignment

    how to call the method to display the requirements.
    What method are you trying to call? Where are you trying to call it from?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Junior Member
    Join Date
    Dec 2013
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help printing output for Contact Manager assignment

    The getContacts method. When you choose option 3 from the menu it should give you the first and last name of each contact with a number to choose to display the details and type of contacts. I'm not sure how to call the method and where in my program to call it from to get it to do this.

Similar Threads

  1. Please Help. Urgent. For loops and printing to output statement.
    By timisaballer23 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 17th, 2013, 08:20 AM
  2. Printing Output from an array in a gui form.
    By chizzo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 28th, 2012, 08:50 AM
  3. [SOLVED] Newbie, why is my program printing output twice?
    By VikingCoder in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 2nd, 2012, 07:47 PM
  4. help with printing output in columns
    By jblankinship in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 14th, 2012, 01:03 PM
  5. need help Printing output from a JTextfield
    By juanbond311 in forum Java Theory & Questions
    Replies: 27
    Last Post: June 21st, 2010, 08:26 AM

Tags for this Thread