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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • satyakarvvk
    New Member
    • Aug 2007
    • 2

    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.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.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    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

    Comment

    • satyakarvvk
      New Member
      • Aug 2007
      • 2

      #3
      Thank you very much sir. Now my code is running successfully... .

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by satyakarvvk
        Thank you very much sir. Now my code is running successfully... .
        Good; you're welcome of course.

        kind regards,

        Jos

        Comment

        Working...