Skip to main content
gamedev.net
🔒 Locked

[java] Getting user input from the console?

Started by phil05 3 replies 950+ views
Original Post
phil05
phil05
Suprisingly, my Java book does not teach this. Is there a simple solution?
phil05
phil05
I looked into it. If anyone is wondering too, here's the code:

// be sure to throw IOExceptionpublic static void main(String args[]) throws IOException// .... in main() later... InputStreamReader inputStreamReader = new InputStreamReader ( System.in );BufferedReader stdin = new BufferedReader ( inputStreamReader );Common PC = new Common();		String sName = stdin.readLine();PC.name = sName;		System.out.println(PC.name);
capn_midnight
capn_midnight
public class Keyboard{    static BufferedInputReader bread=new BufferedInputReader(new InputStreamReader(System.in);    public static String readLine(){        try{            return bread.readLine();        }        catch(IOException e){            return null;        }    }}

I don't know what this "Common" class is, but you don't need it. And main shouldn't throw the exception, you should personally catch it.
phil05
phil05
Oh, Common just the player's class. Thanks for the code.
Captain Logic
Captain Logic
If you are using JDK 1.5 check out the Scanner class for a much simplified way of reading input:

Scanner sc = new Scanner(System.in);
String s = sc.next();

returns a whitespace delimited string entered at a command prompt.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.