You can also read from the console, altho this requires a bit of extra code to get everything working.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// introductory/ConsoleInputOutput.java // Michael Maus, 28 August 2004 // Write to and read from the console. import java.io.*; public class ConsoleInputOutput { public static void main(String[] args) throws IOException { String name; // Declare a variable to hold the name. BufferedReader bufReader; // Declare a BufferedReader variable. bufReader = new BufferedReader(new InputStreamReader(System.in)); System.out.println("What's your name, Earthling?"); name = bufReader.readLine(); // Read one line from the console. System.out.println("Take me to your leader, " + name); System.exit(0); // Not necessary, but commonly used. } } |
import java.io.*
IOException
), we have to say that we might
give it (throw it) to the method that called up.
BufferedReader
.
readLine()
method to get the next input line.