0

So I was solving the stdin and stdout II question on Hackerrank. Code attached below:

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc = new Scanner(System.in);
        int i = sc.nextInt();
        double d = sc.nextDouble();
        sc.nextLine(); 
        String s = sc.nextLine();
        sc.close();
        //printing the output
        System.out.println("String: " + s);
        System.out.println("Double: " + d);
        System.out.println("Int: " + i);
    }
}

And it always skipped the String s = sc.nextLine(); if I did not use sc.nextLine(); after every numerical input. The only thing I know is that I have to add that line there. But I have no idea. Can anybody please explain to me why such aberrant behavior happens?

Pratik Thorat
  • 403
  • 3
  • 7

0 Answers0