BlueJ and Input from keyboard

George V picture George V · Apr 27, 2015 · Viewed 9.9k times · Source

i have written the following code:

import java.io.*;
public class Typer
{
    public static void main(String[] args) 
    {
       Console cons;
       cons = System.console();
       boolean edition = true;
       if(cons == null)
       {
           edition = false;
       }
       if(edition)
       {
           String name = cons.readLine("Give your name: ");
           System.out.println("Your name is: "+ name);
       }
       else
       {
           System.out.println("There is no console!");
       }
    }
 }

i am using BlueJ and it doesn't prompt for an input. it just prints out there is no console! Any thougts? Thanks you!

When i compile and run the program at powershell it runs normally. the thing is different with bluej for some reason.

Answer

Samreen Fatima picture Samreen Fatima · Aug 11, 2015

To input any value using BlueJ, I normally use the BufferedReader statement. It goes like this, BufferedReader name=new BufferedReader(new InputStreamReader(System.in)); After writing this statement in the class or the method, you can input any value using the console. Be sure to give this statement after asking for a value. In case of integers- int variable name= Integer.parseInt(name.readLine);