[英]NumberFormat Exception during string parsing in Java
我正在为一个问题编写解决方案,该问题包括除其他外,解析一个字符串,该字符串包含用户输入的数字,用空格分隔,然后将它们存储为整数。
当我使用nextLine()
方法首先接受字符串(包括空格),然后使用split
方法分离整数时,我不确定为什么会出现数字格式异常。 仍然很奇怪的是,该代码以前曾在另一个问题中起作用,但显然不在这里。
这是代码,以及异常消息:
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);
}
}
}
以及异常消息:
Enter number of test cases:
2
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
Enter N, C, M separated by spaces
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:592)
at java.lang.Integer.parseInt(Integer.java:615)
at algorithms.Warmup.ChocolateFeastTest.main(ChocolateFeastTest.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Process finished with exit code 1
在跟踪异常时,当我使用Integer.parseInt()
时,似乎在该行中发生了错误,但是即使在此之前,为什么代码仍未首先读取数字(带空格)呢? 即:此行不起作用:
String next = sc.nextLine()
我将不胜感激!
您正在使用nextInt()
,它仅读取整数,而不读取行尾的换行符\\n
。
因此,当您按下整数然后输入时,该行:
int T = sc.nextInt();
只读取整数。 接下来,当您这样做时:
String next = sc.nextLine();
它读取在输入中等待读取的换行符。
只需更改为:
int T = Integer.parseInt(sc.nextLine());
(但是,当然,尝试/捕捉会更好)
问题是nextInt()
不使用全行,因此当您执行String next = sc.nextLine();
它读取同一行,从而导致错误。
问题可以通过解决
int T = sc.nextInt();
nextLine(); //adding a next line after nextInt()
我只是改变了: int T = sc.nextInt();
至: 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);
}
}
}
您需要两次调用“ sc.nextLine()”,以便从下一行获取输入。 第一个电话将带您到下一行,第二个电话将在第二行上获取输入。
String next = sc.nextLine();
next = sc.nextLine();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.