| Author |
Problem with PrintWriter and another Problem with Inputhandling
|
Daniel Siepmann
Greenhorn
Joined: Jun 18, 2010
Posts: 3
|
|
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.
|
Project: http://kenai.com/projects/bauelemente
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
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.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Daniel Siepmann
Greenhorn
Joined: Jun 18, 2010
Posts: 3
|
|
Big thanks. I set everything to Locale.US.
Does someone have an Idea for my 2nd "problem"?
|
 |
Peter Taucher
Ranch Hand
Joined: Nov 18, 2006
Posts: 174
|
|
Something like this could work:
|
Censorship is the younger of two shameful sisters, the older one bears the name inquisition.
-- Johann Nepomuk Nestroy
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Or use the hasXXX methods:
|
 |
 |
|
|
subject: Problem with PrintWriter and another Problem with Inputhandling
|
|
|