The error is near a=Integer.parseInt(next_split[0]);
What is happening? Why the error?
error: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 JavaLoops.main(JavaLoops.java:(line_number_in_my_code)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class JavaLoops {
public static void main(String[] args) throws Exception {
int t,a,b,n;
Scanner in = new Scanner(System.in);
t= in.nextInt();
for(int i=0;i<t;i++)
{
String input=in.nextLine();
String[] next_split = input.split(" ");
System.out.println(next_split[0]);
a=Integer.parseInt(next_split[0]);
b=Integer.parseInt(next_split[1]);
n=Integer.parseInt(next_split[2]);
calculate(a,b,n);
}
}
static void calculate(int a,int b,int n)
{
int constant=a+((int) Math.pow(2,0)*b);
System.out.print(constant+" ");
int res=0;
for(int i=1;i<n;i++)
{
res=constant+((int) Math.pow(2,i)*b);
constant=res;
System.out.print(res+" ");
}
}
}
This is because the position of scanner after in.nextInt(); will be just after your input integer.
in.nextLine(); inside the loop will then read nothing.
Solution either
1) Make the position for scanner to next new line
t= Integer.parseInt(in.nextLine());
2) Have a new Scanner Object
Scanner in2 = new Scanner(System.in);
String input=in2.nextLine();