English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

write a program which accepts a numeric value from the user, If a user enters an alphanumeric value, the program throws an exception and display an error message

2006-07-16 21:38:54 · 1 answers · asked by chippy 1 in Computers & Internet Programming & Design

1 answers

Accept the input as a String()

Then convert to the required format using parse as follows.
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Double.html#parseDouble(java.lang.String)
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Float.html#parseFloat(java.lang.String)
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Short.html#parseShort(java.lang.String)
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Integer.html#parseInt(java.lang.String)
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Long.html#parseLong(java.lang.String)

You will notice that these all throw a NumberFormatException

2006-07-17 00:13:36 · answer #1 · answered by AnalProgrammer 7 · 0 0

fedest.com, questions and answers