469,270 Members | 1,117 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

java.lang.NumberFormatException: For input string: ""

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, again
i will ask user whether he want to continue again and the process is repeated.
My code is working first time normally but after user says yes(y) for continuing again my code producing an error. The Integer.parseInt taking space as default string and producing java.lang.NumberFormatException: For input string: "".
So please help me out, this is my code: in this i created objects using array variables as iam repeating the loop.


import java.io.*;

class Oddnos
{
private int n;

Oddnos(int n)
{
this.n=n;
}

void odds()
{
System.out.println();

for(int i=1;i<=n;i++)
{
if(i%2!=0)
System.out.print(" "+i);
}
}
}

class UOdd
{
static int count=0,l=0;
public static void main(String args[])throws IOException,NumberFormatException
{

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Oddnos[] obj = new Oddnos[50];

while(count>=0)
{
System.out.print("Enter the no. upto which u want the odd no's: ");
obj[l] = new Oddnos(Integer.parseInt(br.readLine()));

obj[l].odds();

System.out.print("\n \n Do u want to continue again (y/n): ");
char x = (char)br.read();

if(x=='y' || x=='Y')
{
++count;
l=count;
}
else
{
count=-1;
l=count;
System.out.println("\n \n Thank u \n");
}
}
}
}

Note: The code produces same error even though i created one object and repeated in the loop.
Aug 19 '07 #1
3 52076
JosAH
11,448 Expert 8TB
When you're asked to continue yes or no you actually type something like this:

Y<enter>

i.e. *two* characters: the Y character and the enter key. The Y character is
read but that newline character is still there as if it were an empty line. The
next call to that readLine() method sees that newline character and assumes
an empty line, hence the "" which is not a valid integer number.

The remedy is simple: invoke that readLine() method after you've read that single
Y (or whatever) character to get rid of that newline character in the input buffer.

kind regards,

Jos
Aug 19 '07 #2
Thank you very much sir. Now my code is running successfully....
Aug 19 '07 #3
JosAH
11,448 Expert 8TB
Thank you very much sir. Now my code is running successfully....
Good; you're welcome of course.

kind regards,

Jos
Aug 19 '07 #4

Post your reply

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

Similar topics

2 posts views Thread by Genzo | last post: by
8 posts views Thread by b83503104 | last post: by
37 posts views Thread by Jan Wagner | last post: by
14 posts views Thread by mlw | last post: by
49 posts views Thread by aarklon | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.