• Post Reply Bookmark Topic Watch Topic
  • New Topic

PrintWriter Not Writing to File  RSS feed

 
Rachel Green
Greenhorn
Posts: 13
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I feel like I've tried every solution Google has to offer. It's probably simple but I just can't get my arraylist to print to a text file. The goal is for the user to enter 5, and for the contents of the arraylist friends to write to a text file. Can someone help me out with this?

 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
an't get my arraylist to print to a text file.

Are the statements on lines 68 to 73 executed?
Is a file named: Friends.txt created?
What is in the file?
 
Rachel Green
Greenhorn
Posts: 13
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those lines are not executed. I've tried creating the file manually to see if it would be accessed and written to but that did not work. And the file is not being created by the program, either. I have the input file in the working directory and that is being opened and contents are being added to my arraylist without a problem. What happens right now is that the user selects 5 and hits enter and the program ends. I've also tried writing the arraylist using the following but this did not work either:
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those lines are not executed.

The code in those lines work for me when I input the single character: "5"
Try adding a print statement after line 35 (just after the do) that prints the value of input:

The > and < are to show if there are any spaces before or after the 5.
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note: If input is changed to "5" inside the if/else if statements  the loop will exit without looping back to the beginning of the if/else if

Change the while() condition to use something that is set after the data is written to the file.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norman is correct. This will only work if "5" was the first command. As a followup command, it will exit immediately, so it can't do the "save" to file.

Henry
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a cow (that's a good thing) for:

  • Using code tags
  • Correct indentation
  • Long lines broken into shorter lines
  •  
    Rachel Green
    Greenhorn
    Posts: 13
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Norman, it's like you just turned on the lights! I've never entered "5" as my first choice because I've always wanted to make edits to the list before testing the PrintWriter function. So I never ran into the "it works one way but not the other" idea. I will make some edits and test this out as soon as I get home today. Thank you to everyone who read this and chimed in. Next, I need to move most of this out of my main() and change my else-if to a switch case.
     
    Knute Snortum
    Sheriff
    Posts: 4279
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Here are a few tips on programming style from your code:

    * It is preferred to write:

    List<String> friends = new ArrayList<>();

    Just List on the left-hand side because it's better to code to the interface rather than the implementation.  And ArrayList<>(); since Java 7 because most of the time Java can determine the type from the context.

    * It is a little safer to write:

    "literal".equals(variable)

    ...since variable could be null.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!