Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to solve numberformatexception and getting input string:""?
    text
    copied!<p>I am trying to make a program where The inputs consist of several lines, with the first line indicating the number of succeeding lines of character sequences. These character sequences will be splitted by " ". And the splitted characters will be converted into integer and get its sum.</p> <pre><code> For Example: 2 2 3 4 5 2 3 Sum is 5 4 5 Sum is 9 </code></pre> <p>Now, When I input the same inputs, i get this..</p> <pre><code> 2 2 3 4 5 Sum is 0 2 3 Sum is 5 4 5 Sum is 9 java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:504) at java.lang.Integer.parseInt(Integer.java:527) at ITweek.sum2(ITweek.java:33) at ITweek.main(ITweek.java:18) </code></pre> <p>Why do I have a return of Sum is 0? and an error of NumberFormatException: For input string: ""? I don't get it. I don't know how and where to find the answer. Any help will be appreciated. Thank you.</p> <p>This is my code..</p> <pre><code> import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Prob { public static void main(String args[]){ Scanner input = new Scanner(System.in); int loop = input.nextInt(); String numberString[]=new String[loop+1]; for(int i=0; i&lt;=loop; i++){ String ans = input.nextLine(); numberString[i] = ans; } sum2(numberString); } static void sum2(String []answers){ for(String b : answers){ System.out.print(""+b+" "); String splittedNumber[] = b.split(" "); int sum = 0; for(String j : splittedNumber){ try{ sum=sum+Integer.parseInt(j); }catch(Exception x){ x.printStackTrace(); } } System.out.println("Sum is "+sum); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload