473,326 Members | 2,680 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

NumberFormatException error! Can some one help me to see what's wrong?

Every time I enter for example 1 mile, I get the error.
Expand|Select|Wrap|Line Numbers
  1. /**
  2.  * This program will convert measurements expressed in inches,
  3.  * feet, yards, or miles into each of the possible units of
  4.  * measure.  The measurement is input by the user, followed by
  5.  * the unit of measure.  For example: "17 feet", "1 inch", 
  6.  * "2.73 mi".  Abbreviations in, ft, yd, and mi are accepted.
  7.  * The program will continue to read and convert measurements
  8.  * until the user enters an input of 0.
  9.  */
  10.  
  11. import java.io.*;
  12. import java.util.*;
  13.  
  14. public class LengthConverter { 
  15.  
  16.     public static void main(String[] args) throws IOException {
  17.  
  18.         double measurement;    // Numerical measurement, input by user.
  19.  
  20.         /* The unit of measure for the input,
  21.          * also specified by the user.
  22.          */
  23.         String units;
  24.  
  25.         /* Measurement expressed in each possible unit of meassure. */
  26.         double inches, feet, yards, miles;
  27.  
  28.  
  29.         String str;
  30.  
  31.  
  32.         // Create a Buffered Reader.
  33.         BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
  34.  
  35.         System.out.print("\nEnter measurements in inches, feet, yards, or miles. \n");
  36.         System.out.println("For example: 1 inch   17 feet   2.73 miles\n\n");
  37.         System.out.println("You can use abbreviations: in   ft   yd   mi\n\n");
  38.         System.out.println("I will convert your input into the other units of measure");
  39.         System.out.println();
  40.  
  41.  
  42.         while (true) {
  43.  
  44.             /* Get the user's input, and convert units to lower case. */
  45.  
  46.  
  47.             str = userInput.readLine();
  48.             measurement = Double.parseDouble(str);
  49.  
  50.  
  51.             if ( measurement == 0)
  52.                 break;        // Terminate the while loop.
  53.  
  54.             units = userInput.readLine();
  55.             units = units.toLowerCase();            
  56.  
  57.  
  58.             /* Convert the input measurement to inches. */
  59.  
  60.             if (units.equals("inch") || units.equals("inches") || units.equals("in")) {
  61.                 inches = measurement;
  62.             }
  63.  
  64.             else if (units.equals("foot") || units.equals("feet") || units.equals("ft")) {
  65.                 inches = measurement * 12;
  66.             }
  67.  
  68.             else if (units.equals("yard") || units.equals("yards") || units.equals("yd")) {
  69.                 inches = measurement * 36;
  70.             }
  71.  
  72.             else if (units.equals("mile") || units.equals("miles") || units.equals("mi")) {
  73.                 inches = measurement * 12 * 5280;
  74.             }
  75.  
  76.             else {
  77.                 System.out.println("Sorry, but I don't understand \"" + units + "\".");
  78.  
  79.                 continue;    // Back to start of while loop.
  80.             }
  81.  
  82.             /* Convert measurement in inches to feet, yards, and miles. */
  83.  
  84.             feet = inches / 12;
  85.             yards = inches / 36;
  86.             miles = inches / (12 * 5280);
  87.  
  88.             /* Output measurement in terms of each unit of measure. */
  89.  
  90.             System.out.println("\nThat's equivalent to: \n");
  91.             System.out.printf("%12.5g", inches);
  92.             System.out.println(" inches");
  93.             System.out.printf("%12.5g", feet);
  94.             System.out.println(" feet");
  95.             System.out.printf("%12.5g", yards);
  96.             System.out.println(" yards");
  97.             System.out.printf("%12.5g", miles);
  98.             System.out.println(" miles");
  99.             System.out.println();
  100.  
  101.         } // end of while loop.
  102.  
  103.         System.out.println();
  104.         System.out.println("OK! Bye for now!!! ");
  105.  
  106.     } // end of main()
  107.  
  108. } // end of class LengthConverter
Sep 27 '09 #1
3 2950
r035198x
13,262 8TB
1.) Please use code tags if you have to post code.
2.)Well "1 mile" is not a double is it? You are going to have to split the number part and the unit of measure part. Either use String.split(" ") to separate them or take them in separately from the user.
Sep 28 '09 #2
Frinavale
9,735 Expert Mod 8TB
You get a NumberFormatException when you try to convert a string that is not a number into a number.

If your string is:

"1mi"

This is obviously not a it's not a number...

You have to do something to extract "1" (which is a number) from the string and convert that value into a number.

If your string is:
"1 mi"

Then you can split the string (as r035198x has suggested) on the space between the 1 and the "mi" to extract the number value.

If there is no space, then you'll have to use a different technique to extract the number value out of the string.
Oct 2 '09 #3
It will obviously produce an NumberFormat exception because you are typing a integer and string.If you just enter a numeric value it will not generate an error
Oct 4 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Aravind | last post by:
we have two files: 1. rc4.c (defines one function "create_pin()") 2. MyImpl.c(calling the function "create_pin()"),This implements JNI method. 1.When I am trying to create .dll file with one...
1
by: yanwan | last post by:
I met this problem in executing a c++ project in visual studio. Does anyone have suggestions to resolve "error lnk 2001"? --------------------Configuration: reconstruction - Win32...
2
by: Randy Harris | last post by:
I thought that I had a grasp of how VBA error handling functioned, but have just become painfully aware that I don't. I thought that the "On Error GoTo 0" in the second sub below would turn off...
6
by: Squirrel | last post by:
I have a command button on a subform to delete a record. The only statement in the subroutine is: DoCmd.RunCommand acCmdDeleteRecord The subform's recordsource is "select * from tblVisit order...
1
by: Minh | last post by:
I've just installed VS.NET 2003 on my Athlon XP 1800+. However I couldn't get any project with STL includes to compile even if I create a new empty project (and added #include <string>). It gave me...
2
by: Qiao Yun | last post by:
I used vc++.net (visual studio .net ) to open a project which can work well in vc++6.0. I succeeded in compiling the project in vc++.net in release mode . But when I tried to compile the project...
7
by: p | last post by:
WE had a Crystal 8 WebApp using vs 2002 which we upgraded to VS2003. I also have Crystal 9 pro on my development machine. The web app runs fine on my dev machine but am having problems deploying....
0
by: bazzer | last post by:
hey, im trying to access a microsoft access database from an ASP.NET web application in visual basic 2003.NET. i get the following error when i try running it: Server Error in...
3
by: satyakarvvk | last post by:
Hi everybody! Please help me to overcome below runtime exception. Actually it is a simple program on basics. I want to print odd nos upto which the user asks and after printing the task,...
1
by: prasanna ganesh | last post by:
why i'm getting lang.string.NumberFormatException for input string ""please help me to find solution
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.