• Post Reply Bookmark Topic Watch Topic
  • New Topic

Program not Writing User Input to File  RSS feed

 
Aras Vitkus
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone, I'm relatively new to Java and new to these forums.

I have a program I am working on, and I can't get the user input to write to the file. I also have this small problem when the user enters input, I have to hit enter twice to continue the loop instead of once. What am I doing wrong and what should I do? Sorry if this code is a complete mess. Here is the main:



Here is the subclass: I haven't gotten to reading from the file and displaying the results to the user yet, I'll try that myself first.



Thanks for any help!
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens if you close the BufferedWriter?
 
Aras Vitkus
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I closed BufferedWriter and the same problem persists. No change.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A couple of problems I notice in your loop.

In the do while, you create a new instance of a BufferedWriter using a new FileWriter. Because this is in the loop, every time the FileWriter is created, the constructor you are using will delete the file if it exists. You can use the second constructor of FileWriter that accepts a boolean as a second parameter to append.

Also you create the variable input and place the output of kb.nextLine() in it. But what you are trying to write to the file is another call to kb.nextLine().
 
Aras Vitkus
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I've made some progress. Thanks for the help so far! Now, the problem is when the user enters multiple strings or a sentence, only the first word is written to the file. Also, the word "exit" is always written at the end of the file. What should I do to fix these?

Here is the updated code:

 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The call to next() in the loop only gets the next token.

Notice that the loop only exits when the value the user entered is "exit". But you write to the file before that check.
 
Aras Vitkus
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again, I fixed the call to next() and I am now getting complete sentences/strings. I moved my if statement around to check for "exit" but to no avail, program runs to infinity. Is there a certain spot it needs to be, or do I need to code a different check somewhere else?
 
Aras Vitkus
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nevermind, got it to work. Thanks!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!