Question

This is my solution for sphere's online judge palin problem. It runs fine on Netbeans, but the judge is rejecting my answer saying it gives a RuntimeError. I tried it on JCreator and it says:

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:468)
    at java.lang.Integer.parseInt(Integer.java:497)
    at Main.main(Main.java:73)

I'm not passing an empty string for it to parse, why is this?

The code:

import java.io.*;
import java.util.*;



class Main {

    public static int firstPalinLargerThanNum(int num){

        int foundPalin =0;

        int evalThisNum = ++num;

        while (true){


        if (isPalin(evalThisNum))


            break;

        evalThisNum++;
        }

        foundPalin = evalThisNum;
        return foundPalin;

    }

    public static boolean isPalin(int evalThisNum){

           boolean isItPalin = false;

           int dig=0;
           int rev=0;


          int  n = evalThisNum;

          while (evalThisNum > 0)
          {

           dig = evalThisNum % 10;
           rev = rev * 10 + dig;
           evalThisNum = evalThisNum / 10;

          }

           if (n == rev) {

               isItPalin=true;
           }

           return isItPalin;

    }


    public static void main(String args[]) throws java.lang.Exception{

        BufferedReader r1 = new BufferedReader(new InputStreamReader(System.in));

        /*BufferedReader r1 = new BufferedReader (new FileReader(new File ("C:\\Documents and Settings\\Administrator\\My Documents\\NetBeansProjects\\Sphere\\src\\sphere\\sphere\\PALIN_INPUT.txt")));*/

        String read = r1.readLine();

        int numberOfTestCases = Integer.parseInt(read);

        for (int i=0; i<numberOfTestCases;i++){

        read = r1.readLine();

        if (read!=null){

        int num = Integer.parseInt(read);

        System.out.println(firstPalinLargerThanNum(num));

        }
        }
    }


}

Input:

2
808
2133

line 73 is: int num = Integer.parseInt(read);

Was it helpful?

Solution

You will get that error if you hit <Enter> when the program is expecting a number.

Suppose your input is

2
3 
<Enter>

You will receive the error you have indicated after processing the number 3, as you have told your routine to iterate twice.

As an aside, on top of error handling around the number parsing, you might also want to introduce a trim() to the readLine() method calls:

String read = r1.readLine().trim();

This will allow you to handle gracefully the input in the event that the user to put in whitespace around the numbers.

OTHER TIPS

Just a wild guess: Could there be a problem with different end-of-line separators. E.g. your program actually gets 2<CR><LF>808<CR><LF>2133<CR><LF>, thinks that the line ends at the <CR> and processes the line.

Now when it tries to process the next line it finds <LF> which makes it think it read an empty String.

You cannot assume that the user knows how to use your program and will give you correct input. The judge probably hit enter, without typing any number. How is he/she supposed to know the input that your program requires? A program should fail gracefully, not blow up in the user's face with cryptic errors.

You should be doing something like the following, so that the user knows what to do:

private static function readInt(BufferedReader reader) throws IOException
{
    boolean done = false;
    int result = -1;
    while ( ! done ){
       System.out.print("Please enter an integer: ");
       String str = reader.readLine();
       try{
          result = Integer.parseInt(str);
          done = true;
       }catch(NumberFormatException cantconvert){
          System.out.println("That isn't an integer. Try again.");
       }
    }
    return result;
}

Additionally, you shouldn't use an exception specifier with the main function (that is, don't use "throws" in the signature of "main"). You should handle those IOExceptions and print a pretty and intelligible message to the user, even if there is nothing you can do about the exception to fix it or make it go away.

I just ran your example code under Eclipse 3.4 without error. I was only able to induce a similar error when I did not provide the specified number of test cases, i.e.:

6
56
87
[Enter]

So I am inclined to agree with akf that there must be an extra Enter happening somewhere, because this error will only be generated when there are insufficient lines of input.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top