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.parseIn t taking space as default string and producing java.lang.Numbe rFormatExceptio n: 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.prin tln();
for(int i=1;i<=n;i++)
{
if(i%2!=0)
System.out.prin t(" "+i);
}
}
}
class UOdd
{
static int count=0,l=0;
public static void main(String args[])throws IOException,Num berFormatExcept ion
{
BufferedReader br = new BufferedReader( new InputStreamRead er(System.in));
Oddnos[] obj = new Oddnos[50];
while(count>=0)
{
System.out.prin t("Enter the no. upto which u want the odd no's: ");
obj[l] = new Oddnos(Integer. parseInt(br.rea dLine()));
obj[l].odds();
System.out.prin t("\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.prin tln("\n \n Thank u \n");
}
}
}
}
Note: The code produces same error even though i created one object and repeated in the loop.
3 52530 JosAH 11,448
Recognized Expert MVP
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
Thank you very much sir. Now my code is running successfully... .
JosAH 11,448
Recognized Expert MVP
Thank you very much sir. Now my code is running successfully... .
Good; you're welcome of course.
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 print it out, the "&lang" gets
converted into some strange character.
i.e., just try:
alert ("heyhey&lang=blahblah");
|
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 header, I found "zh"
is not standard. I found this line in apache2's default httpd.conf
# Simplified Chinese (zh-CN)
AddLanguage zh-CN .zh-cn
|
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):
|
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 works great
except this one value, i'm getting
|
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 required for a
screen reader (text to speech) for accessibility, to help the reader
software use the correct presentation method (e.g. correct language
module).
| |
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 places on application
code.
Take, for instance, this C++ construct:
class foo
{
char *m_name;
|
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 workplace, office supplies, music CDs, DVD movies, or software).
• Create a product class that holds the item number, the name of the product, the number of units in stock, and the price of each unit.
• Create a Java application that displays the...
|
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
|
by: prasanna ganesh |
last post by:
why i'm getting lang.string.NumberFormatException for input string ""please help me to find solution
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |