0
votes
int[] a = new int[3];

        String s = e.nextLine();
        String[] sa = s.split(" ");
        for (int i = 0;i<sa.length;i++){
            a[i]=Integer.parseInt(sa[i]);
        }

I could not find any issue. Getting this error...

Exception in thread "main" java.lang.NumberFormatException: For input string: "" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68) at java.base/java.lang.Integer.parseInt(Integer.java:662) at java.base/java.lang.Integer.parseInt(Integer.java:770) at Team.main(Team.java:23)

I think my Intellij IDEA is having some trouble. Does this error appear, if IDE does not work well?

1
an empty String is not a valid numerical value, that is the whole problem. This has nothing at all to do with your IDE - Stultuske
That is a String array...."sa" i have printed the array to see its working or not and its working...its printing an array - Elham Nusrat
yes, but one of the elements is an empty String. And when you try to parse that to a numeric value, that's when you get that error - Stultuske
Thanks for your suggestion but I don't think so....the split(" ") ignoring all the empty Strings/spaces...isnt it? - Elham Nusrat
no. it doesn't ignore it, it adds empty Strings as elements to the result array - Stultuske

1 Answers

0
votes

The problem is that your string has two or more blank spaces one after the other so when you do the split(" ") it will return you empty strings between those spaces.

You have to remove the empty strings before trying to convert to integer. You can do that using an if statement:

int j = 0;
for (int i = 0;i<sa.length;i++){
     if(!sa[i].isEmpty()){
            a[j]=Integer.parseInt(sa[i]);
            j++;
     }
}