Q:

How to solve java.lang.NumberFormatException: empty String

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)

A:

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/

