Original Post
🔒 Locked
[java] Getting user input from the console?
Advertisement
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);
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.
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.
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.
Advertisement