1

I was making a program to take user input and passing it out but for some reason, the scanner class and next line don't even wait for the user to take input, instead, it just skips the whole take input part. Here is my code:

public class Demo{
    public static void main(String[] args){
        
        Scanner scanGenre = new Scanner("System.in");
        System.out.println("Enter genre: ");
        String genre = scanGenre.nextLine();
    }
}

Basically, the program just ends without me typing any input, and I couldn't find a solution or explanation to this.

Mohil Patel
  • 437
  • 3
  • 9
Luke
  • 65
  • 4
  • 3
    The quick rush to mark something as a duplicate based solely on a question's title BUGS THE HECK OUT OF ME! This is NOT a question that is answered by the supposed duplicate. That answer has to do with reading from `System.in` (not a string) and doing a `sc.nextLine` after a `sc.nextInt`, a classic problem many programmers hit. This is something else. I DO think this should be closed, but the reason is "you have a typo", not that it's answered by the other question. Advice to those wishing to "close due to duplicate"...check to see if the two questions are really the same...Please! – CryptoFool Jan 23 '21 at 04:26
  • @Steve Yeah, they do that :( Reopen vote cast :) – Red Jan 24 '21 at 03:43

2 Answers2

1

Remove the double quotes around System.in

The double quotes cause your parameter to be represented as a string but you want it to be represented as an InputStream.

1

Use the following:

Scanner scanGenre = new Scanner(System.in);
System.out.println("Enter genre: ");
String genre = scanGenre.nextLine();

You need to remove the double quotes around "System.in," because otherwise the compiler would assume it as a String, rather than an Object (InputStream).

Spectric
  • 30,714
  • 6
  • 20
  • 43