Hello,

Can anyone help me solve the error in this program??
I get the following error:

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at converter.main(basic_convert.java:20)

The whole program runs properly. The error come after asking if I want to calculate again or not. The error come or 'y' but not for 'n'. I use eclipse 3.5 SDK.


Thanks

Edited 6 Years Ago by aakaashjois: n/a

import java.io.*;
class converter
{
	public static void main(String args[])throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		System.out.println("What can I call you");
		String name=br.readLine();
		int in;
		char option = 0;
		do
		{
			System.out.println("What do you want to convert "+name);
			System.out.println("1)Distance/Length");
			System.out.println("2)Weight/Mass");
			System.out.println("3)Temperature");
			System.out.println("4)Time");
			System.out.println("5)Area");
			System.out.println("6)Volume");
			in=Integer.parseInt(br.readLine());
			if(in==1)
			{
				System.out.println("Please give the input to convert");
				double input=Double.parseDouble(br.readLine());
				System.out.println("Is you input in ");
				System.out.println("1)Milimeters, mm");
				System.out.println("2)Centimeters, cm");
				System.out.println("3)Meters, m");
				System.out.println("4)Kilometers, km");
				System.out.println("5)Inches, in");
				System.out.println("6)Feet, ft");
				System.out.println("7)Miles, mile");
				int type=Integer.parseInt(br.readLine());
				if(type==1)
				{
					System.out.println("The input you entered is "+input+" mm");
					System.out.println("Converting......");
					try
			        {
			            Thread.sleep(5000);
			        }
			        catch (Throwable t){}
			        System.out.println(input+" mm = "+(0.1*input)+" cm");
			        System.out.println();
			        System.out.println(input+" mm = "+(0.001*input)+" m");
			        System.out.println();
			        System.out.println(input+" mm = "+(0.0000001*input)+" km");
			        System.out.println();
			        System.out.println(input+" mm = "+(0.039*input)+" in");
			        System.out.println();
			        System.out.println(input+" mm = "+(0.0032*input)+" ft");
			        System.out.println();
			        System.out.println(input+" mm = "+(0.00000006*input)+" mile");
			        System.out.println();
				}
				else if(type==2)
				{
					System.out.println("The input you entered is "+input+" cm");
					System.out.println("Converting......");
					try
			        {
			            Thread.sleep(5000);
			        }
			        catch (Throwable t){}
			        System.out.println(input+" cm = "+(10*input)+" mm");
			        System.out.println();
			        System.out.println(input+" cm = "+(0.01*input)+" m");
			        System.out.println();
			        System.out.println(input+" cm = "+(0.000001*input)+" km");
			        System.out.println();
			        System.out.println(input+" cm = "+(0.39*input)+" in");
			        System.out.println();
			        System.out.println(input+" cm = "+(0.032*input)+" ft");
			        System.out.println();
			        System.out.println(input+" cm = "+(0.0000006*input)+" mile");
			        System.out.println();
				}
				else if(type==3)
				{
					System.out.println("The input you entered is "+input+" m");
					System.out.println("Converting......");
					try
			        {
			            Thread.sleep(5000);
			        }
			        catch (Throwable t){}
			        System.out.println(input+" m = "+(1000*input)+" mm");
			        System.out.println();
			        System.out.println(input+" m = "+(100*input)+" cm");
			        System.out.println();
			        System.out.println(input+" m = "+(0.001*input)+" km");
			        System.out.println();
			        System.out.println(input+" m = "+(39.37*input)+" in");
			        System.out.println();
			        System.out.println(input+" m = "+(3.28*input)+" ft");
			        System.out.println();
			        System.out.println(input+" m = "+(0.0006*input)+" mile");
			        System.out.println();
				}
				else if(type==4)
				{
					System.out.println("The input you entered is "+input+" km");
					System.out.println("Converting......");
					try
			        {
			            Thread.sleep(5000);
			        }
			        catch (Throwable t){}
			        System.out.println(input+" km = "+(1000000*input)+" mm");
			        System.out.println();
			        System.out.println(input+" km = "+(100000*input)+" cm");
			        System.out.println();
			        System.out.println(input+" km = "+(1000*input)+" m");
			        System.out.println();
			        System.out.println(input+" km = "+(39370.07*input)+" in");
			        System.out.println();
			        System.out.println(input+" km = "+(3280.83*input)+" ft");
			        System.out.println();
			        System.out.println(input+" km = "+(0.6213*input)+" mile");
			        System.out.println();
				}
				else if(type==5)
				{
					System.out.println("The input you entered is "+input+" in");
					System.out.println("Converting......");
					try
			        {
			            Thread.sleep(5000);
			        }
			        catch (Throwable t){}
			        System.out.println(input+" in = "+(25.4*input)+" mm");
			        System.out.println();
			        System.out.println(input+" in = "+(2.54*input)+" cm");
			        System.out.println();
			        System.out.println(input+" in = "+(0.025*input)+" m");
			        System.out.println();
			        System.out.println(input+" in = "+(0.0000025*input)+" km");
			        System.out.println();
			        System.out.println(input+" in = "+(0.083*input)+" ft");
			        System.out.println();
			        System.out.println(input+" in = "+(1.578*input)+" mile");
			        System.out.println();
				}
				else if(type==6)
				{
					System.out.println("The input you entered is "+input+" ft");
					System.out.println("Converting......");
					try
			        {
			            Thread.sleep(5000);
			        }
			        catch (Throwable t){}
			        System.out.println(input+" ft = "+(304.8*input)+" mm");
			        System.out.println();
			        System.out.println(input+" ft = "+(30.48*input)+" cm");
			        System.out.println();
			        System.out.println(input+" ft = "+(0.304*input)+" m");
			        System.out.println();
			        System.out.println(input+" ft = "+(0.0003*input)+" km");
			        System.out.println();
			        System.out.println(input+" ft = "+(12*input)+" ft");
			        System.out.println();
			        System.out.println(input+" ft = "+(0.00018*input)+" mile");
			        System.out.println();
				}
				else if(type==7)
				{
					System.out.println("The input you entered is "+input+" mile");
					System.out.println("Converting......");
					try
			        {
			            Thread.sleep(5000);
			        }
			        catch (Throwable t){}
			        System.out.println(input+" mile = "+(1609344*input)+" mm");
			        System.out.println();
			        System.out.println(input+" mile = "+(16093.4*input)+" cm");
			        System.out.println();
			        System.out.println(input+" mile = "+(1609.34*input)+" m");
			        System.out.println();
			        System.out.println(input+" mile = "+(1.609*input)+" km");
			        System.out.println();
			        System.out.println(input+" mile = "+(63360*input)+" in");
			        System.out.println();
			        System.out.println(input+" mile = "+(5280*input)+" ft");
			        System.out.println();
				}
			}
			else if(in==2)
			{
				System.out.println("Give the input to convert");
				double input=Double.parseDouble(br.readLine());
				System.out.println("Is you input in");
				System.out.println("1) Miligrams, mg");
				System.out.println("2) Grams, g");
				System.out.println("3) Kilograms, kg");
				System.out.println("4) Pounds, lb");
				int type=Integer.parseInt(br.readLine());
				if(type==1)
				{
					System.out.println("The input you entered is "+input+" mg");
					System.out.println("Converting......");
					try
			        {
			            Thread.sleep(5000);
			        }
			        catch (Throwable t){}
			        System.out.println(input+" mg = "+(0.001*input)+" g");
			        System.out.println();
			        System.out.println(input+" mg = "+(0.0000001*input)+ " kg");
			        System.out.println();
			        System.out.println(input+" mg = "+(0.0000002*input)+ "lb");
			        System.out.println();
				}
				else if(type==2)
				{
					System.out.println("The input you entered is "+input+" g");
					System.out.println("Converting......");
					try
			        {
			            Thread.sleep(5000);
			        }
			        catch (Throwable t){}
			        System.out.println(input+" g = "+(1000*input)+" mg");
			        System.out.println();
			        System.out.println(input+" g = "+(0.001*input)+ " kg");
			        System.out.println();
			        System.out.println(input+" g = "+(0.002*input)+ "lb");
			        System.out.println();
				}
				else if(type==3)
				{
					System.out.println("The input you entered is "+input+" kg");
					System.out.println("Converting......");
					try
			        {
			            Thread.sleep(5000);
			        }
			        catch (Throwable t){}
			        System.out.println(input+" kg = "+(100000*input)+" mg");
			        System.out.println();
			        System.out.println(input+" kg = "+(1000*input)+ " g");
			        System.out.println();
			        System.out.println(input+" kg = "+(2.204*input)+ "lb");
			        System.out.println();
				}
				else if(type==4)
				{
					System.out.println("The input you entered is "+input+" lb");
					System.out.println("Converting......");
					try
			        {
			            Thread.sleep(5000);
			        }
			        catch (Throwable t){}
			        System.out.println(input+" lb = "+(453592.3*input)+" mg");
			        System.out.println();
			        System.out.println(input+" lb = "+(453.5*input)+ " g");
			        System.out.println();
			        System.out.println(input+" lb = "+(0.45*input)+ "kg");
			        System.out.println();
				}
			}
			else if(in==3)
			{
				System.out.println("Give the input to convert");
				double input=Double.parseDouble(br.readLine());
				System.out.println("Is your input in ");
				System.out.println("1)Fahrenheit,°F");
				System.out.println("2)Celcius, °C");
				System.out.println("3)Kelvin, K");
				int type=Integer.parseInt(br.readLine());
				if(type==1)
				{
					System.out.println("The input you entered is "+" °F");
					System.out.println("Converting......");
					try
					{
						Thread.sleep(5000);
					}
					catch(Throwable t){}
					System.out.println(input+" °F = "+(-17.2*input)+" °C");
					System.out.println();
					System.out.println(input+" °F = "+(255.9*input)+" K");
					System.out.println();
				}
				else if(type==2)
				{
					System.out.println("The input you entered is "+" °C");
					System.out.println("Converting......");
					try
					{
						Thread.sleep(5000);
					}
					catch(Throwable t){}
					System.out.println(input+" °C = "+(33.8*input)+" °F");
					System.out.println();
					System.out.println(input+" °C = "+(274.15*input)+" K");
					System.out.println();
				}
				else if(type==3)
				{
					System.out.println("The input you entered is "+" K");
					System.out.println("Converting......");
					try
					{
						Thread.sleep(5000);
					}
					catch(Throwable t){}
					System.out.println(input+" K = "+(-457.87*input)+" °F");
					System.out.println();
					System.out.println(input+" K = "+(-272.15*input)+" °C");
					System.out.println();
				}
			}
			else if(in==4)
			{
				System.out.println("Give the input to convert");
				double input=Double.parseDouble(br.readLine());
				System.out.println("Is your input in ");
				System.out.println("1)Milliseconds, ms");
				System.out.println("2)Seconds, s");
				System.out.println("3)Minutes, min");
				System.out.println("4)Hours, h");
				System.out.println("5)Days, days");
				System.out.println("6)Weeks, weeks");
				System.out.println("7)Months, months");
				System.out.println("8)Years, years");
				int type=Integer.parseInt(br.readLine());
				if(type==1)
				{
					System.out.println("The input you entered is "+" ms");
					System.out.println("Converting......");
					try
					{
						Thread.sleep(5000);
					}
					catch(Throwable t){}
					System.out.println(input+" ms = "+(0.001*input)+" s");
					System.out.println();
					System.out.println(input+" ms = "+(0.00000016*input)+" min");
					System.out.println();
					System.out.println(input+" ms = "+(0.000000027*input)+" h");
					System.out.println();
					System.out.println(input+" ms = "+(0.0000000011*input)+" days");
					System.out.println();
					System.out.println(input+" ms = "+(0.0000000001*input)+" weeks");
					System.out.println();
					System.out.println(input+" ms = "+(0.00000000003*input)+" months");
					System.out.println();
					System.out.println(input+" ms = "+(0.000000000003*input)+" years");
					System.out.println();
				}
				if(type==2)
				{
					System.out.println("The input you entered is "+" s");
					System.out.println("Converting......");
					try
					{
						Thread.sleep(5000);
					}
					catch(Throwable t){}
					System.out.println(input+" s = "+(1000*input)+" ms");
					System.out.println();
					System.out.println(input+" s = "+(0.016*input)+" min");
					System.out.println();
					System.out.println(input+" s = "+(0.0002*input)+" h");
					System.out.println();
					System.out.println(input+" s = "+(0.0000015*input)+" days");
					System.out.println();
					System.out.println(input+" s = "+(0.00000016*input)+" weeks");
					System.out.println();
					System.out.println(input+" s = "+(0.000000038*input)+" months");
					System.out.println();
					System.out.println(input+" s = "+(0.0000000031*input)+" years");
					System.out.println();
				}
				if(type==3)
				{
					System.out.println("The input you entered is "+" min");
					System.out.println("Converting......");
					try
					{
						Thread.sleep(5000);
					}
					catch(Throwable t){}
					System.out.println(input+" min = "+(60000*input)+" ms");
					System.out.println();
					System.out.println(input+" min = "+(60*input)+" s");
					System.out.println();
					System.out.println(input+" min = "+(0.016*input)+" h");
					System.out.println();
					System.out.println(input+" min = "+(0.0006*input)+" days");
					System.out.println();
					System.out.println(input+" min = "+(0.000009*input)+" weeks");
					System.out.println();
					System.out.println(input+" min = "+(0.000002*input)+" months");
					System.out.println();
					System.out.println(input+" min = "+(0.0000001*input)+" years");
					System.out.println();
				}
				if(type==4)
				{
					System.out.println("The input you entered is "+" h");
					System.out.println("Converting......");
					try
					{
						Thread.sleep(5000);
					}
					catch(Throwable t){}
					System.out.println(input+" h = "+(3600000*input)+" ms");
					System.out.println();
					System.out.println(input+" h = "+(3600*input)+" s");
					System.out.println();
					System.out.println(input+" h = "+(60*input)+" min");
					System.out.println();
					System.out.println(input+" h = "+(0.041*input)+" days");
					System.out.println();
					System.out.println(input+" h = "+(0.005*input)+" weeks");
					System.out.println();
					System.out.println(input+" h = "+(0.001*input)+" months");
					System.out.println();
					System.out.println(input+" h = "+(0.0001*input)+" years");
					System.out.println();
				}
				if(type==5)
				{
					System.out.println("The input you entered is "+" days");
					System.out.println("Converting......");
					try
					{
						Thread.sleep(5000);
					}
					catch(Throwable t){}
					System.out.println(input+" days = "+(60000*input)+" ms");
					System.out.println();
					System.out.println(input+" days = "+(60*input)+" s");
					System.out.println();
					System.out.println(input+" days = "+(0.016*input)+" mins");
					System.out.println();
					System.out.println(input+" days = "+(0.0006*input)+" h");
					System.out.println();
					System.out.println(input+" days = "+(0.000009*input)+" weeks");
					System.out.println();
					System.out.println(input+" days = "+(0.000002*input)+" months");
					System.out.println();
					System.out.println(input+" days = "+(0.0000001*input)+" years");
					System.out.println();
				}
				if(type==6)
				{
					System.out.println("The input you entered is "+" weeks");
					System.out.println("Converting......");
					try
					{
						Thread.sleep(5000);
					}
					catch(Throwable t){}
					System.out.println(input+" weeks = "+(604799999.9*input)+" ms");
					System.out.println();
					System.out.println(input+" weeks = "+(6047999*input)+" s");
					System.out.println();
					System.out.println(input+" weeks = "+(10080*input)+" mins");
					System.out.println();
					System.out.println(input+" weeks = "+(168*input)+" h");
					System.out.println();
					System.out.println(input+" weeks = "+(7*input)+" days");
					System.out.println();
					System.out.println(input+" weeks = "+(0.231*input)+" months");
					System.out.println();
					System.out.println(input+" weeks = "+(0.01*input)+" years");
					System.out.println();
				}
				if(type==7)
				{
					System.out.println("The input you entered is "+" months");
					System.out.println("Converting......");
					try
					{
						Thread.sleep(5000);
					}
					catch(Throwable t){}
					System.out.println(input+" months = "+(262800000*10*input)+" ms");
					System.out.println();
					System.out.println(input+" months = "+(2627999.9*input)+" s");
					System.out.println();
					System.out.println(input+" months = "+(43799.9*input)+" mins");
					System.out.println();
					System.out.println(input+" months = "+(730*input)+" h");
					System.out.println();
					System.out.println(input+" months = "+(30.41*input)+" days");
					System.out.println();
					System.out.println(input+" months = "+(4.34*input)+" weeks");
					System.out.println();
					System.out.println(input+" months = "+(0.083*input)+" years");
					System.out.println();
				}
				if(type==8)
				{
					System.out.println("The input you entered is "+" years");
					System.out.println("Converting......");
					try
					{
						Thread.sleep(5000);
					}
					catch(Throwable t){}
					System.out.println(input+" years = "+(315360000*100*input)+" ms");
					System.out.println();
					System.out.println(input+" years = "+(31535999.9*input)+" s");
					System.out.println();
					System.out.println(input+" years = "+(525599.9*input)+" mins");
					System.out.println();
					System.out.println(input+" years = "+(8760*input)+" h");
					System.out.println();
					System.out.println(input+" years = "+(365*input)+" days");
					System.out.println();
					System.out.println(input+" years = "+(52.14*input)+" weeks");
					System.out.println();
					System.out.println(input+" years = "+(12*input)+" months");
					System.out.println();
				}
			}
			if(in==5)
			{
				System.out.println("Please enter the input to convert");
				double input=Double.parseDouble(br.readLine());
				System.out.println("Is your input in ");
				System.out.println("1)Milimeter square, mm²");
				System.out.println("2)Centimeter square, cm²");
				System.out.println("3)Meter square, m²");
				System.out.println("4)Kilometer square, km²");
				System.out.println("5)Hectare, ha²");
				int type=Integer.parseInt(br.readLine());
				if(type==1)
				{
					System.out.println("The input you entered is "+input+" mm²");
					System.out.println("Converting......");
					try
					{
						Thread.sleep(5000);
					}
					catch(Throwable t){}
					System.out.println(input+" mm² = "+(0.01*input)+" cm²");
					System.out.println();
					System.out.println(input+" mm² = "+(0.0000001*input)+" m²");
					System.out.println();
					System.out.println(input+" mm² = "+(0.0000000000001*input)+" km²");
					System.out.println();
					System.out.println(input+" mm² = "+(0.00000000001*input)+" ha");
					System.out.println();
				}
				if(type==2)
				{
					System.out.println("The input you entered is "+input+" cm²");
					System.out.println("Converting......");
					try
					{
						Thread.sleep(5000);
					}
					catch(Throwable t){}
					System.out.println(input+" cm² = "+(100*input)+" mm²");
					System.out.println();
					System.out.println(input+" cm² = "+(0.0001*input)+" m²");
					System.out.println();
					System.out.println(input+" cm² = "+(0.000000000001*input)+" km²");
					System.out.println();
					System.out.println(input+" cm² = "+(0.000000001*input)+" ha");
					System.out.println();
				}
				if(type==3)
				{
					System.out.println("The input you entered is "+input+" m²");
					System.out.println("Converting......");
					try
					{
						Thread.sleep(1000);
					}
					catch(Throwable t){}
					System.out.println(input+" m² = "+(1000000*input)+" mm²");
					System.out.println();
					System.out.println(input+" m² = "+(10000*input)+" cm²");
					System.out.println();
					System.out.println(input+" m² = "+(0.0000001*input)+" km²");
					System.out.println();
					System.out.println(input+" m² = "+(0.0001*input)+" ha");
					System.out.println();
				}
				if(type==4)
				{
					System.out.println("The input you entered is "+input+" km²");
					System.out.println("Converting......");
					try
					{
						Thread.sleep(5000);
					}
					catch(Throwable t){}
					System.out.println(input+" km² = "+(10000000*100000*input)+" mm²");
					System.out.println();
					System.out.println(input+" km² = "+(10000000*1000*input)+" cm²");
					System.out.println();
					System.out.println(input+" km² = "+(1000000*input)+" m²");
					System.out.println();
					System.out.println(input+" km² = "+(100*input)+" ha");
					System.out.println();
				}
				if(type==5)
				{
					System.out.println("The input you entered is "+input+" ha");
					System.out.println("Converting......");
					try
					{
						Thread.sleep(5000);
					}
					catch(Throwable t){}
					System.out.println(input+" ha = "+(1000000*10000*input)+" mm²");
					System.out.println();
					System.out.println(input+" ha = "+(10000000*1000*input)+" cm²");
					System.out.println();
					System.out.println(input+" ha = "+(10000*input)+" m²");
					System.out.println();
					System.out.println(input+" ha = "+(0.01*input)+" km²");
					System.out.println();
				}
			}
			if(in==6)
			{
				System.out.println("Give the input to convert");
				double input=Double.parseDouble(br.readLine());
				System.out.println("Is your input in ");
				System.out.println("1)Mililiters, mL");
				System.out.println("2)Liters, L");
				System.out.println("3)Gallons, gallons");
				int type=Integer.parseInt(br.readLine());
				if(type==1)
				{
					System.out.println("The input you entered is "+" mL");
					System.out.println("Converting......");
					try
					{
						Thread.sleep(5000);
					}
					catch(Throwable t){}
					System.out.println(input+" mL = "+(0.001*input)+" L");
					System.out.println();
					System.out.println(input+" °F = "+(0.00026*input)+" gallons");
					System.out.println();
				}
				else if(type==2)
				{
					System.out.println("The input you entered is "+" L");
					System.out.println("Converting......");
					try
					{
						Thread.sleep(5000);
					}
					catch(Throwable t){}
					System.out.println(input+" L = "+(1000*input)+" mL");
					System.out.println();
					System.out.println(input+" L = "+(0.26*input)+" gallons");
					System.out.println();
				}
				else if(type==3)
				{
					System.out.println("The input you entered is "+" gallons");
					System.out.println("Converting......");
					try
					{
						Thread.sleep(5000);
					}
					catch(Throwable t){}
					System.out.println(input+" gallons = "+(3785.41*input)+" mL");
					System.out.println();
					System.out.println(input+" gallons = "+(3.78*input)+" L");
					System.out.println();
			}
			}
			System.out.println("Do you want to calculate anything else?(y/n)");
			option=(char)br.read();
			}
		
		while(option=='y');
		System.out.println("Thank you for using K()NVERTr");
		System.out.println("This program is done by Aakaash");
		System.exit(0);
		}
}

The exception tells you where the error is:
at converter.main(basic_convert.java:20)

You are calling Integer.parseInt with argument an empty String: "" As the exception says:
java.lang.NumberFormatException: For input string: ""

Try to read it first before posting this entire, unrelated piece of code

As I said you are calling the Integer.parseInt with argument an empty String: "" , as the exception says.

So how does the argument of that method takes value? Shouldn't you put it in a try-catch block?

I didn't read the entire code but from what I can understand, you are entering from the keyboard a value that is not a number.
I just saw that you are using:
br.read() , instead of br.readLine() when you read the 'y'/'n' input. Why?

When you call the read method, you don't "change lines" so the next time you call readLine, it will not read the next line but the rest of the current.

You enter >'y' and you use read (doesn't change line)
The next time you call readLine, the cursor is still at the line: >'y'
And reads what is after: an empty String: ""

Throughout your program you call readLine:
>123
You read the number and the next you will cal any read method it will read the next line.

But with simple read, you read the line but you don't change lines in order to read the next.

Try calling all the time the readLine.

I thought the char datatype does not support readLine

I didn't look at the API, but you are probably right.

If that is the case then you can use String:

String option="";

....
do {

  option = br.readLine();
} while(option.equals("y"));

Or you can do this:

System.out.println("Do you want to calculate anything else?(y/n)");
  option=(char)br.read();

  [B]br.readLine();[/B]

} while(option=='y');

With that extra call you leave the line where the 'y' was entered.