0

Why do I need to put sc.nextLine();? I tried without it, but the string just could not catch my input.

import java.util.Scanner;
public class Solution {

    public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            System.out.println("Please enter integer, double and string");  
            int x=sc.nextInt();
            double y=sc.nextDouble();
             sc.nextLine();
            String s=sc.nextLine();
            //Complete this code

            System.out.println("String: "+s);
            System.out.println("Double: "+y);
            System.out.println("Int: "+x);
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 2
    Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – maloomeister Oct 13 '21 at 13:06
  • The linked duplicate explains the issue and answers your question. – maloomeister Oct 13 '21 at 13:07

0 Answers0