5 Replies - 630 Views - Last Post: 15 December 2013 - 07:24 PM Rate Topic: -----

#1 Strong1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 13-December 13

File output not working but no error messages.

Posted 13 December 2013 - 06:54 PM

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.");

}



Any idea why the contacts will not print? All I get is the number of contacts in records when I choose this option. I would greatly appreciate any help!

This post has been edited by macosxnerd101: 13 December 2013 - 07:04 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: File output not working but no error messages.

#2 ChrisNt  Icon User is offline

  • cute bug

Reputation: 212
  • View blog
  • Posts: 648
  • Joined: 31-July 13

Re: File output not working but no error messages.

Posted 13 December 2013 - 08:53 PM

method printStackTrace will show your the error.

catch (FileNotFoundException e) {
       e.printStackTrace();
}

Was This Post Helpful? 0
  • +
  • -

#3 Strong1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 13-December 13

Re: File output not working but no error messages.

Posted 14 December 2013 - 07:11 PM

printStackTrace doesn't do anything when I add it to my code.
Was This Post Helpful? 0
  • +
  • -

#4 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: File output not working but no error messages.

Posted 15 December 2013 - 07:34 AM

Why doesn't your TreeSet have generics? TreeSet<String>(). Also, can we see your csv file content?
Was This Post Helpful? 0
  • +
  • -

#5 Strong1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 13-December 13

Re: File output not working but no error messages.

Posted 15 December 2013 - 07:10 PM

the csv is showing null for each record so the problem is with the file writer instead of with printing the output right?

Here is that code:

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) {
    }
        }


Was This Post Helpful? 0
  • +
  • -

#6 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: File output not working but no error messages.

Posted 15 December 2013 - 07:24 PM

Without running your code, I know for certain you need to have a finally { } after the catch to .flush() and .close() the writer. Otherwise, the file is not closed. Contact is a complex object I assume--Does it have an overriden toString() method that returns its attributes (first and last name I presume) as a String?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1