This is how we got taught in first year comp sci: Code: static String readln(String message) { // this is the method that reads a String from the terminal / console BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String line = ""; System.out.println(message); try { line = reader.readLine(); } catch (IOException e){ System.out.println("Failed to read a String, error: " + e); } // try catch return line; } // readln This'll read the input line by line. Just call the function when ever you want a line read, parse it a string, and it'll prompt the user for it. e.g. Code: //in a method String name = readln("Input your first name."); Outputs: Input your first name. [waits for input, and for the user to hit the enter key]
thanks that last line is what i needed (not into error handeling yet) ya its in my book somewhere but for the life of me i cant find it. thanks EDIT: im using readln() but im gettting an error uppon compile. Code: --------------------Configuration: numberConverter - j2sdk1.4.1 <Default>-------------------- C:\Program Files\Xinox Software\JCreator LE\MyProjects\Current project\numberConverter\numberConverter.java:64: cannot resolve symbol symbol : method readln (java.lang.String) location: class numberConverter input1 = readln("Enter first number"); ^ 1 error Process completed.
Did you define the method readln() somewhere in your class? The most important part about what the harlequin gave in code is not the line that says: String name = readln("Input your first name."); It's the part where he coded the readln() method. All that last line is, is in another method somewhere you are asking for input from the user. Here is a good article telling you what the harlequin told you, but with a little explanation of each step. BTW, that was just from a Google for "Java take input from command line".
so thers nothing like <STDIN> (perl) i can use? im just looking for a way to test my logic without getting the GUI in place and dealing with an event handler (its being a PITA). Its not exactly appealing to me to write annother method just to take input. (im actually writing this in applet form so ide have to fool arround with init and such) but if this is the ony way.?.?.?
In it's most basic form, all you need to write is: Code: BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String var_name = reader.readLine(); I think that'll work
Compiler wants me to catch or throw the exception (neither of which i know how to do) i liker perls way muvh better