Hey everyone.

rather new to java and was wondering on how i would accomplish this.

I am using the Netbeans JDE in creating a Java GUI.
I have completed the whole script but now am just doing some fine tuning in flattening out any errors that could go wrong with the script such as the user inputing a character rather than a numeric value.

As this program is a currency converter is must also allow for the user to input a decimal place, could anyone please explain to me how to use this?

cheers,
luke.

I'm not sure if there is a way to restrict JTextBox to only numeric values. What I would do is have the user input the data, then parse what every they input as a double. When you do that the decimal will be considered part of the number. When you parse, check for a NumberFormatException, if one is thrown the user most likely inputed letters, ask them again for the data.

rather new to java

I am using the Netbeans JDE in creating a Java GUI.

If you are new to Java then do not directly use GUI designers provided in NetBeans, you might get your initial form designing work done faster but when it comes to do some actual coding with these UI components you will find yourself paralyzed. So begining use a minimalistic IDE (eg JCreator LE) which allows you to just compile and run your code and nothing else (not even code completion). And in case you run into any problems or want to look for methods provided by the built-in java classes refer to the javadocs either online or they can be downloaded from here.

Now as far as your problem is concerned, If I am not wrong you must be having some sort of "button" so the user can tell your application to convert whatever value you have in the JTextField to the specified currency, So over there itself you can put a validation check to ensure whether the user has entered a proper numeric value or not. You can just shoot a getText() method on your JTextField and using the matches() method of the String class in combination with regular expressions, to check if a valid input has been specified.
Alternatively you could also implement the KeyListener to check the value in the JTextField as it is typed on the fly. You can learn more about even handling in Java here.

Before performing any calculations on the value from the text field just check using a regex whether every character in the text field is either a digit or a period (.) character. You could form the regex so that you can restrict the number of digits before and after the decimal point.

Try to parse the number as a double. If an exception is thrown clear the text field and request that the user input a valid decimal number.

Try to parse the number as a double. If an exception is thrown clear the text field and request that the user input a valid decimal number.

Read the other posts before posting. That is exactly what I said.

hi everyone, thanks for the responses.i found the easiest way was to do the try and catch statements when parsing the double.

although i tried inputing the line

jTextField2.setText("Currency Converter accepts numbers only.");

and it would not set the text as that.
then tried that in the catch statement putting

System.exit(0);

and the program would terminate.

is there any way i can get around this??

is there any way i can get around this??

I presume you are trying to ask the user for input again? You should be able to set the text in the text field, but without seeing your code I don't know why it wouldn't work. Use a while loop to keep asking for input.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.