尝试使用 Integer.parseInt(next_split[0]) 时出错;
Error while trying to use Integer.parseInt(next_split[0]);
错误接近 a=Integer.parseInt(next_split[0]);
怎么了?为什么会出错?
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+" ");
}
}
}
这是因为in.nextInt()之后扫描仪的位置;将在您输入的整数之后。
in.nextLine();然后在循环内部什么也读不到。
解决方案
1) 将扫描仪定位到下一行
t= Integer.parseInt(in.nextLine());
2) 有一个新的扫描器对象
Scanner in2 = new Scanner(System.in);
String input=in2.nextLine();
只需在for循环中创建新的Scanner对象
for(int i=0;i<t;i++)
{
Scanner s = new Scanner(System.in);
String input=s.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);
}
错误接近 a=Integer.parseInt(next_split[0]);
怎么了?为什么会出错?
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+" ");
}
}
}
这是因为in.nextInt()之后扫描仪的位置;将在您输入的整数之后。
in.nextLine();然后在循环内部什么也读不到。
解决方案
1) 将扫描仪定位到下一行
t= Integer.parseInt(in.nextLine());
2) 有一个新的扫描器对象
Scanner in2 = new Scanner(System.in);
String input=in2.nextLine();
只需在for循环中创建新的Scanner对象
for(int i=0;i<t;i++)
{
Scanner s = new Scanner(System.in);
String input=s.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);
}