473,226 Members | 1,359 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,226 software developers and data experts.

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 52480
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

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

Similar topics

2
by: Genzo | last post by:
Hi all, was wondering if anyone might have knowledge on this. am testing with Opera 7.11. essentially whenever i try to use the substring "&lang=" in any string value in javascript, and try to...
6
by: Zhang Weiwu | last post by:
Hello. I am working with a php software project, in it (www.egroupware.org) Chinese simplified locate is "zh" while Traditional Chinese "tw". I wish to send correct language attribute in http...
8
by: b83503104 | last post by:
Hi, I want to accept the user's answer yes or no. If I do this: answer = input('y or n?') and type y on the keyboard, python complains Traceback (most recent call last):
2
by: chongo | last post by:
Hi, i'm reading in a bigint value from SQL Server. string s = myReader.ToString()); int intBackColour = System.Convert.ToInt32(s); I'm also reading in other values the same way and it...
37
by: Jan Wagner | last post by:
Hi, can't figure this one out, what's the CSS way to specify the language? In HTML it would be simply an lang="xx" attribute, or XHTML xml:lang="xx", but, how about in CSS? This would be...
14
by: mlw | last post by:
Do not take anything about this, it is not a flame or troll, while I'm not new to Java I favor C++. However, I may need to use it in a contract position, and am concerned that the restrictions it...
11
by: hamiltongreg | last post by:
I am new to Java and am having problems getting my program to compile correctly. My assignment is as follows; Choose a product that lends itself to an inventory (for example, products at your...
49
by: aarklon | last post by:
Hi all, See:- http://www.cs.princeton.edu/introcs/faq/c2java.html for C vs Java in number crunching http://husnusensoy.blogspot.com/2006/06/c-vs-java-in-number-crunching.html
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: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.