0

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.

2 Answers 2

2

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]);
Sign up to request clarification or add additional context in comments.

3 Comments

i gave the input: 2 4 12345 3 1234
@gnsb If really that was the input received, your split would work. Please print inp just before parsing it and tell me the output.
printing variables to test the program made me realize the mistake: i had read past the end of file Thanks a lot for the help :)
1

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

1 Comment

Yes, that was the case. Thanks :) About the escape character: If you are using an escaped construct within a string literal, you must precede the backslash with another backslash for the string to compile. (taken from link )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.