• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with PrintWriter and another Problem with Inputhandling

 
Daniel Siepmann
Greenhorn
Posts: 5
Mac PHP VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'm new to Java and a "jung" boy from Germany. So sorry for my bad english.

Ok, I've got two Problems:
1.
I save the Informations from my Program to a txt-File.

This saves the float-value (getPrice()) with a dot. For example 14.5
When I load the File I use this Code, same like getting the user input:

This want the float with a comma. For example 14,5.
I think Scanner uses the language configuration of the OS. PrintWriter uses english. How can I solve this problem?

My 2nd Problem is:
The user can make an Input and I want to handle wrong inputs. The actual code:

Of course there are Exceptions, for example at Line 07. The user writes characters... I can catch them, but I want to display the Error, and the user will get a 2nd chance to make his input. So I think I need a loop, but how can I write this?

For further informations you can take a look at the ProjectWiki. See my signature. There is a Mercurial repository and the last stable version of the program.
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) check out Scanner.useLocale to change the locale to Locale.US.

2) when writing use a NumberFormat to write the float, or use PrintWriter.printf when writing:
Personally, I would probably use a combination of both so you always know that, no matter the system, the output matches the input. And I also wouldn't use Locale.getDefault() then but a specific one like Locale.US or Locale.GERMAN.
 
Daniel Siepmann
Greenhorn
Posts: 5
Mac PHP VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Big thanks. I set everything to Locale.US.

Does someone have an Idea for my 2nd "problem"?
 
Peter Taucher
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something like this could work:
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or use the hasXXX methods:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic