To output to screen: // prints new line character after text System.out.println("text_here" + "more_text_here") // does not print a new line character after text System.out.print("text_here" + "more_text_here") // for a more sophisticated "formatted printing", // theThere are two problems with the input code above:C
-styleprintf
// is available. Input is more complicated. The methodSystem.in.read()
reads one character at a time, too primitive for most users. Perhaps the simplest is to usereadStream
objects. CLICK HERE for more information. What follows are mostly deprecated stuff!! To read an input (deprecated version): // Be sure to import java.io.*. DataInputStream in = new DataInputStream(System.in); String answer; System.out.print("Quit? (Type "Y" or "y")"); System.out.flush(); response = in.readLine(); if(!response.equals("Y") && !response.equals("y")) ...
DataInputStream
by BufferedReader
, as illustrated here:
The old version
should become:DataInputStream d = new DataInputStream(in);
Here is the program illustrating the above code. Unfortunately, this does not compile in Microsoft world. Also notice how the two Hello World Programs represent two different ways of catching IO exceptions.BufferedReader d = new BufferedReader(new InputStreamReader(in));
Link to Sun's I/O Tutorial: