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.
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.
Comment