gnsb
gnsb

Reputation: 326

Exception in thread "main" java.lang.NumberFormatException: For input string: "" (I entered digits, but it seems to have read an empty string)

There is something wrong in this section:

BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
String faaltu = cin.readLine();
String inp = cin.readLine();
String[] part = inp.split("\\s");

for(int k = 0; k < part.length; k++)
{
    System.out.println(part[k]);
}

obj.Smax = Integer.parseInt(part[0]);

I gave the following input:

2
4 12345
3 1234

Here is the complete code:

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

/* Name of the class has to be "Main" only if the class is public. */
class Codejam
{
    Codejam(){};
    static Codejam obj = new Codejam();
    int totalStanding = 0;
    int T;//no of test cases
    int[] S;// no of people at each given shyness level
    boolean[] standing;
    int Smax;
    int total = 0, newInv = 0;

    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
        obj.T = Integer.parseInt(cin.readLine());
        for(int i = 0; i < obj.T; i++)
        {
            obj.populate();
            obj.update();
            while (obj.totalStanding < obj.total)
            {
                obj.newInv++;
                obj.S[0]++;
                obj.update();
            }
            System.out.println("Case #" + i + ": " + obj.newInv);
        }


    }

    public void update()
    {
        for(int i = 0;i < obj.S.length; i++)
        {
            if ((totalStanding >= i) && (obj.standing[i] == false) )
            {
                obj.totalStanding += obj.S[i];
                obj.standing[i] = true;
            }
        }
    }
    public void populate() throws IOException
    {
        BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
        String faaltu = cin.readLine();
        String inp = cin.readLine();
        String[] part = inp.split("\\s");

        for(int k = 0; k < part.length; k++)
        {
            System.out.println(part[k]);
        }

        obj.Smax = Integer.parseInt(part[0]);
        obj.S = new int[Smax + 1]; 
        obj.standing = new boolean[Smax + 1];
        for(int j = 0;j < part[1].length(); j++)
        {
            obj.S[j] = part[1].charAt(j) - '0';
            obj.total += S[j];

        }

    }

}

and got exception

Exception in thread "main" java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at Codejam.populate(Codejam.java:57) at Codejam.main(Codejam.java:24)

Please point out where I have gone wrong.

Upvotes: 0

Views: 10988

Answers (2)

redge
redge

Reputation: 1192

It looks to me like you are trying to read too many lines from your input.

I suspect you are reading past the end of file.

I suggest you pass cin.readline () to your populate() method as an argument so you don't have to open another reader.

You also shouldn't have to escape your split expression so heavily I think it currently reads "a backslash and an s" rather than "whitespace".

Upvotes: 1

Jean-Fran&#231;ois Savard
Jean-Fran&#231;ois Savard

Reputation: 21004

The error is pretty clear, you tried to parse to a number an empty string which thrown an exception.

We have no idea what is your input, but part[0] is the empty string causing the error :

obj.Smax = Integer.parseInt(part[0]);

Upvotes: 2

Related Questions