• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

PrintWriter Not Writing to File

 
Greenhorn
Posts: 13
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 7125
    184
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic