如何解决 java.lang.NumberFormatException: 空字符串

huangapple go评论84阅读模式
英文:

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&lt;Double&gt; T = new ArrayList&lt;Double&gt;();
		List&lt;Double&gt; R = new ArrayList&lt;Double&gt;();
		String[] Q = new String[2] ;
		
		System.out.println(&quot;Enter the number of entries&quot;);
		n = s.nextInt();
		if(n&lt;= 1) {
			System.out.println(&quot;The number of observations should be greater than one, Try again!&quot;);
			return;
		}
		else {
			System.out.println(&quot;Make sure that there is only one whitespace between each number on a line&quot;);
		}
		
		for(int i=0;i&lt;=n;i++) {
			number = s.nextLine();
			Q = number.split(&quot;\\s&quot;);
			
			T.add(Double.parseDouble(Q[0]));
			R.add(Double.parseDouble(Q[1]));	
		}
		for(int i=0;i&lt;=T.size();i++ ) {
			for(int j=0;j&lt;=R.size();j++) {
				if(i==j) {
					c= R.get(i)/T.get(i);
					if(c&gt;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 &quot;main&quot; 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/

huangapple
  • 本文由 发表于 2020年8月23日 15:51:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63544620.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定