Home > Software engineering >  NetBeans IDE console does not work with user inputs (Java scanner class)
NetBeans IDE console does not work with user inputs (Java scanner class)

Time:12-03

Hello Everyone I am writing simple program that accepts user inputs with java Scanner class from the java.util package. But when I click the console to type something it is not accepting the keyboards input(it does not show the keyboard inputs) , what's wrong with the console? or the IDE itself ? any solution please

import java.util.Scanner; // import the Scanner class 

class Main {
  public static void main(String[] args) {
    Scanner myObj = new Scanner(System.in);
    String userName;
    
    // Enter username and press Enter
    System.out.println("Enter username"); 
    userName = myObj.nextLine();   
       
    System.out.println("Username is: "   userName);        
  }
}

[the program](https://img.codepudding.com/202212/8660dd2fd9b94264b938c3aa148918d5.png)[the console](https://i.stack.imgur.com/JP89p.png)

CodePudding user response:

It may be an issue with the IDE or the console itself. You can try running the program in a different IDE or in the command line to see if the issue persists.

Alternatively, you can try using the System.console() method to retrieve the console input stream directly, like this:

Scanner myObj = new Scanner(System.console().reader());

This should also allow the Scanner to properly read input from the console.

  • Related