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.
For Example:
2
2 3
4 5
2 3 Sum is 5
4 5 Sum is 9
Now, When I input the same inputs, i get this..
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)
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.
This is my 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<=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);
}
}
}
Your problem, obviously ;), it's in this part of code
Scanner input = new Scanner(System.in);
int loop = input.nextInt();
String numberString[]=new String[loop]; //remove last block of array. +1 is useless
for(int i=0; i<=loop; i++){
String ans = input.nextLine();
numberString[i] = ans;
}
sum2(numberString);
}
You read from the standard input. First you read an int and pass it to loop variable. Your error is in thinking that after reading the int, you jump to the next line, which is not true. After reading an int, your buffer is ready to start reading right after the first char.
Basically, your first input in the numberString array is a \n character. If you want to mix readInt and readLine, be sure that after readInt you indicate to read the rest of the line to let the buffer advance. You can either use readLine for both cases or use a readLine after readInt and throw the \n to the garbage.
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int loop = Integer.parseInt(input.readLine());
String numberString[]=new String[loop]; //remove last block of array. +1 is useless
for(int i=0; i<loop; i++){
String ans = input.nextLine();
numberString[i] = ans;
}
sum2(numberString);
or
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int loop = input.readInt());
input.readLine;
String numberString[]=new String[loop]; //remove last block of array. +1 is useless
for(int i=0; i<loop; i++){
String ans = input.nextLine();
numberString[i] = ans;
}
sum2(numberString);