By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,584 Members | 1,750 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,584 IT Pros & Developers. It's quick & easy.

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

P: 1
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
Share this Question
Share on Google+
3 Replies


10K+
P: 13,264
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
Expert Mod 5K+
P: 9,731
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

P: 12
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

Post your reply

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