• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java formatting trouble  RSS feed

 
Jayden Brymatt
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I've got this program to work except for one thing. I can't seem to get the amounts to write to the text file with 2 numbers following the decimal, this would reference . I've tried everything I can think of and have looked at enough pages on the internet that it is all starting to run together. Can anyone nudge me in the right direction?

Thanks!

 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you are getting the number and printing it exactly as it is stored. Instead, you need to do some formatting when you print it.

I would take a look a the printf method of the PrintWriter class. I think you would use it instead of println.
 
Jayden Brymatt
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Golden, thank you sir! Hate those off-by-one errors. I thought I had done that, but apparently, i did something else.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good grief! Why are you throwing and catching the same exception? Never do that. Actually there is no need for any exceptions there at all. All you need to do is print an error message and get your loop to repeat. You might find a do loop more appropriate for entry of file names.
You realise that using \\ and \\\\ makes your program Windows‑specific? You can get the correct file separator for your current OS with String fileSeparator = System.getProperty("file.separator");
You can check whether I have given the correct name ("file.separator") because Henry Wong recently kindly provided a link to the Java Tutorials where they are listed.
Alternative code:-There is no need to replace the Unix file separator (=/) with \\; if you use / then nothing will happen. The == null test will never return true, so you have unreachable code there.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!