Java中字符串解析期间的NumberFormat异常
我正在为一个问题编写解决方案,其中包括解析一个字符串,该字符串包含用户输入的数字,用空格分隔,然后将它们存储为整数 当我使用Java中字符串解析期间的NumberFormat异常,java,string,parsing,Java,String,Parsing,我正在为一个问题编写解决方案,其中包括解析一个字符串,该字符串包含用户输入的数字,用空格分隔,然后将它们存储为整数 当我使用nextLine()方法首先接受字符串(包括空格),然后使用split方法分离整数时,我不确定为什么会出现numberformat异常。仍然奇怪的是,代码以前在不同的问题中工作过,但显然不是在这里 以下是代码和异常消息: package algorithms.Warmup; import java.util.ArrayList; import java.util.Scan
nextLine()
方法首先接受字符串(包括空格),然后使用split
方法分离整数时,我不确定为什么会出现numberformat异常。仍然奇怪的是,代码以前在不同的问题中工作过,但显然不是在这里
以下是代码和异常消息:
package algorithms.Warmup;
import java.util.ArrayList;
import java.util.Scanner;
/**
* Created by manishgiri on 4/8/15.
*/
public class ChocolateFeastTest {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Enter number of test cases:");
int T = sc.nextInt();
ArrayList<Integer> result = new ArrayList<>(T);
for(int i = 0; i < T; i++) {
int[] numbers = new int[3];
System.out.println("Enter N, C, M separated by spaces");
String next = sc.nextLine();
String[] nextSplit = next.split(" ");
int item;
for(int p = 0; p < 3; p++) {
item = Integer.parseInt(nextSplit[p]);
numbers[p] = item;
}
int N = numbers[0];
int C = numbers[1];
int M = numbers[2];
System.out.println(N + C + M);
}
}
}
在跟踪异常时,当我使用Integer.parseInt()
时,错误似乎发生在行中,但即使在此之前,为什么代码不首先读取数字(带空格)?ie:这行不行:
String next=sc.nextLine()
我将感谢任何帮助 您使用的是nextInt()
,它只读取整数,而不读取行尾的新行字符\n
因此,当按整数然后输入时,行:
int T = sc.nextInt();
只读取整数。下一步,当您这样做时:
String next = sc.nextLine();
它读取输入中等待读取的新行字符
只需更改为:
int T = Integer.parseInt(sc.nextLine());
(当然,做try/catch-on会更好)问题是
nextLine()
没有使用完整的行,所以当你做String-next=sc.nextLine()代码>它读取导致错误的同一行
问题可以通过以下方法解决
int T = sc.nextInt();
nextLine(); //adding a next line after nextInt()
我刚刚更改:intt=sc.nextInt()代码>
To:int T=Integer.parseInt(sc.nextLine())代码>
这似乎很有效。我尝试使用BufferedReader
,效果很好。
代码如下:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;
public class ChocolateFeastTest {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) throws IOException {
System.out.println("Enter number of test cases:");
int T = sc.nextInt();
ArrayList<Integer> result = new ArrayList<>(T);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for(int i = 0; i < T; i++) {
int[] numbers = new int[3];
System.out.println("Enter N, C, M separated by spaces");
String next = br.readLine();
String[] nextSplit = next.split(" ");
int item;
for(int p = 0; p < 3; p++) {
item = Integer.parseInt(nextSplit[p]);
numbers[p] = item;
}
int N = numbers[0];
int C = numbers[1];
int M = numbers[2];
System.out.println(N + C + M);
}
}
}
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入java.util.Scanner;
公共级巧克力大餐测试{
静态扫描仪sc=新扫描仪(System.in);
公共静态void main(字符串[]args)引发IOException{
System.out.println(“输入测试用例数:”);
int T=sc.nextInt();
ArrayList结果=新的ArrayList(T);
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
for(int i=0;i
您需要调用“sc.nextLine()”两次,以便从下一行获取输入。第一个电话将带您到下一行,第二个电话将获取第二行的输入
String next = sc.nextLine();
next = sc.nextLine();
我首先检查String[]nextSplit=next.split(“”)的元素数量当您只提供一个字符时,code>会产生最佳猜测:nextInt
不会在2
之后使用换行符,因此nextLine
函数最终会读取第一行的其余部分(仅为空)。谢谢!这就是问题所在。@Kick,我对这个网站很陌生,我真的不明白你在那里做了什么
String next = sc.nextLine();
next = sc.nextLine();