[英]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.