英文:
How to solve java.lang.NumberFormatException: empty String
问题
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Problem_S1 {
public static void main(String[] args) {
int n;
Double c=0.0, e=0.0;
String number = null;
Scanner s = new Scanner(System.in);
List<Double> T = new ArrayList<Double>();
List<Double> R = new ArrayList<Double>();
String[] Q = new String[2] ;
System.out.println("输入条目数");
n = s.nextInt();
if(n<= 1) {
System.out.println("观察次数应大于1,请重试!");
return;
}
else {
System.out.println("请确保每行数字之间只有一个空格");
}
for(int i=0;i<=n;i++) {
number = s.nextLine();
Q = number.split("\\s");
T.add(Double.parseDouble(Q[0]));
R.add(Double.parseDouble(Q[1]));
}
for(int i=0;i<=T.size();i++ ) {
for(int j=0;j<=R.size();j++) {
if(i==j) {
c= R.get(i)/T.get(i);
if(c>e){
e=c;
}
}
}
}
System.out.println(e);
}
}
第一次输入后,我会得到一个println,然后错误直接弹出。我甚至不能为字符串数组 Q 输入所需的输入。这是控制台上发生的情况。
输入条目数
3
请确保每行数字之间只有一个空格
Exception in thread "main" java.lang.NumberFormatException: 空字符串
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at ccc2020.Problem_S1.main(Problem_S1.java:35)
英文:
the code below keeps giving java.lang.NumberFormatException: empty String but I can't understand the reason why.
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Problem_S1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int n;
Double c=0.0, e=0.0;
String number = null;
Scanner s = new Scanner(System.in);
List<Double> T = new ArrayList<Double>();
List<Double> R = new ArrayList<Double>();
String[] Q = new String[2] ;
System.out.println("Enter the number of entries");
n = s.nextInt();
if(n<= 1) {
System.out.println("The number of observations should be greater than one, Try again!");
return;
}
else {
System.out.println("Make sure that there is only one whitespace between each number on a line");
}
for(int i=0;i<=n;i++) {
number = s.nextLine();
Q = number.split("\\s");
T.add(Double.parseDouble(Q[0]));
R.add(Double.parseDouble(Q[1]));
}
for(int i=0;i<=T.size();i++ ) {
for(int j=0;j<=R.size();j++) {
if(i==j) {
c= R.get(i)/T.get(i);
if(c>e){
e=c;
}
}
}
}
System.out.println(e);
}
}
After entering the first input, I get the println but then, the error directly pops up. I can't even enter the required input for the string array Q. This is what happens on the console.
Enter the number of entries
3
Make sure that there is only one whitespace between each number on a line
Exception in thread "main" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at ccc2020.Problem_S1.main(Problem_S1.java:35)
答案1
得分: 0
这是因为Scanner.nextInt方法不会读取输入中的换行符号,因此调用Scanner.nextLine在读取换行符号后立即返回。
解决方法是在s.nextInt()之后加入s.nextLine()。
详细的解释可以在以下链接中找到:
https://www.geeksforgeeks.org/why-is-scanner-skipping-nextline-after-use-of-other-next-functions/
英文:
That's because the Scanner.nextInt method does not read the newline character in your input and so the call to Scanner.nextLine returns after reading that newline.
Workaround is that you put s.nextLine() after s.nextInt().
Its well explained over given link
https://www.geeksforgeeks.org/why-is-scanner-skipping-nextline-after-use-of-other-next-functions/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论