• Post Reply Bookmark Topic Watch Topic
  • New Topic

BufferedWriter not printing the results that I wanted  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all!
I've been figuring out this error for a few hours, but couldn't seem to solve the error. Pardon if my explaination isn't good, but I really need some guidance/help right now.

Currently, my program converts Long values to String. And when I test it out, it do print out the correct output. However, when the converted String value is passed over to be written in a text file, it seems that BufferedWriter isn't printing out the outcome that it's supposed to be.


Now the problem is here...It only prints out the last digit of the String value (instead of 79723172).


Here is my FileWriter/BufferedWriter part.


Why is that when I run my program using command prompt, it prints out the output that I wanted, but however when it comes to writing to the file, it doesn't come out right. Any guide/tips/solutions about this?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You just need the FileWriter here
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you post some actual code which demonstrates the problem? Not just a code fragment like what you posted, because there's no way for us to know what else there is which actually causes the problem.

And please don't post pictures of your code -- because then we can't download it and try it ourselves. Paste the actual code in your post and surround it with the "Code" tags.
 
Siti Aishah
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ohh, i'm so sorry..
Anyway here's my code.



Secret.txt contains a 7 digit string. (4358946)
Username and saltvalue will be saved in salt.txt with a format of username:saltvalue

The expected output should be printing out an 8 digit string , but instead, only the last digit will be saved in the text file.
 
Paul Clapham
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know where those expectations came from.



The first of those lines of code produces a random integer between 1 and 9. The second line converts it to a String, which can only have one character. And that's what you see in your output.
 
Siti Aishah
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But how come when I try to test out the value of saltVs (which is in line 44) by printing it out, it does show 8 digit characters. But when I wanted to write it out to a text file (line52), it became a character instead?

Correct me if I'm wrong, but from what I know/understand, isn't the 8 characters is already stored in saltVs and when I wanted to write to a file, I could just call the variable (line 52)?
 
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Siti Aishah wrote:But how come when I try to test out the value of saltVs (which is in line 44) by printing it out, it does show 8 digit characters.

Because you print one character at a time, 8 times.

But when I wanted to write it out to a text file (line52), it became a character instead?

Because you one write to the file once, after the loop, after the last assignment.

Correct me if I'm wrong, but from what I know/understand, isn't the 8 characters is already stored in saltVs and when I wanted to write to a file, I could just call the variable (line 52)?

Nope. You are re-assigning the variable with each loop, so you don't store 8 characters in saltVs, you store one character in saltVs 8 times. Only the last one is kept. Instead of re-assigning the variable you should accumulate the values into a single String (or StringBuffer, or character array).
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, why are those fields marked static?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!