Basic Input and Output in Java
Using the JMUConsole Class |
Prof. David Bernstein |
Computer Science Department |
bernstdh@jmu.edu |
JMUConsole
(you'll have to
download the .class
file
into the directory containing your source code)
"
character is used to denote a
String
literal. So, how can one include
a "
character in a String
?
\"
(which is a single char
)"She said \"None for me, thanks.\" and left."
\
character it
too must be escaped (i.e., \\
)\n
for newline\r
for carriage return\t
for tabString
variablesString
literalsString
literal to a String
variable+
can be used to concatenate
its two operands (one of which must be a String
)
and evaluates to
a String
int number; String course, department; number = 149; department = "CS"; course = department + number;
JMUConsole
JMUConsole.open()
exactly once, before you can use
the JMUConsole
for input or output
JMUConsole.close()
after you are done (i.e., just before the program terminates)
JMUConsole.print(value)
prints the value (whatever type it is)JMUConsole.println(value)
prints the
value followed by a line separatorJMUConsole.printf(Format, value[, value]...)
prints one or more values formatted according to
the Format stringString
literalsboolean
char
String
/** * An example that illustrates the use of the printf() * method in the JMUConsole class * * @author Prof. David Bernstein, James Madison University * @version 1.0 */ public class PrintfExample { /** * The entry point of the application * * @param args The command-line arguments */ public static void main(String[] args) { int age, weight; double monday, tuesday, wednesday; age = 27; // Prepare the JMUConsole for input/output JMUConsole.open(); JMUConsole.printf("%d\n", age); JMUConsole.printf("%10d\n", age); weight = 155; JMUConsole.printf("Prof. B. is %d years old\n", age); JMUConsole.printf("Age: %d\tWeight: %d\n", age, weight); monday = 35.8; tuesday = 0.0; wednesday = -10.2; JMUConsole.println("\nWith No Flags, Width or Precision"); JMUConsole.printf("%f\n", monday); JMUConsole.printf("%f\n", tuesday); JMUConsole.printf("%f\n", wednesday); JMUConsole.println("\nWith No Flags"); JMUConsole.printf("%5.1f\n", monday); JMUConsole.printf("%5.1f\n", tuesday); JMUConsole.printf("%5.1f\n", wednesday); JMUConsole.println("\nWith + Flags"); JMUConsole.printf("%+5.1f\n", monday); JMUConsole.printf("%+5.1f\n", tuesday); JMUConsole.printf("%+5.1f\n", wednesday); // Close the JMUConsole JMUConsole.close(); } }
nextDouble()
, nextInt()
double
value or int
valuenextLine()
String
(i.e., what the user has typed up to the next line
delimiter)next()
String
(i.e., what the user has typed up to the next token
delimiter)/** * An example that illustrates the use of the JMUConsole class * for (output and) input. * * @author Prof. David Bernstein, James Madison University * @version 1.0 */ public class InputExample { /** * The entry point of the application * * @param args The command-line arguments */ public static void main(String[] args) { double weight; int age; // Prepare the JMUConsole for input/output JMUConsole.open(); // Prompt the user for her/his age JMUConsole.print("Enter your age (in years): "); // Read the response age = JMUConsole.nextInt(); // Prompt the user for her/his weight and read the response JMUConsole.print("Enter your weight (in pounds): "); weight = JMUConsole.nextDouble(); // Echo the responses JMUConsole.printf("You are %d years old and weight %5.1f pounds.\n", age, weight); // Close the JMUConsole JMUConsole.close(); } }
System.out.print()
,
System.out.printf()
, etc... JMUConsole.print()
, JMUConsole.printf()
,
etc... work the same way but don't use the out
object which you don't yet understand
Scanner
class
and methods like nextInt()
,
nextDouble()
, etc... JMUConsole.nextInt()
,
JMUConsole.nextDouble()
,
etc... work the same way but don't require the use of
new
and an assignment to an object that
you don't yet understand (but does require that you call
open()
instead)/** * An example that illustrates an important consideration when * using the next___() methods. * * @author Prof. David Bernstein, James Madison University * @version 1.0 */ public class InconvenienceExample { public static void main(String[] args) { int age; String name; // If you run this program you won't be able to enter your // name because the return you enter after your age will // be considered the "next line" JMUConsole.open(); JMUConsole.print("Enter your age: "); age = JMUConsole.nextInt(); JMUConsole.print("Enter your name: "); name = JMUConsole.nextLine(); JMUConsole.close(); } }
readDouble()
, readInt()
double
value or int
value and "consume" the newline characterreadLine()
nextLine()
/** * An example that illustrates the convenience of the * JMUConsole class (as compared to the Scanner class). * * @author Prof. David Bernstein, James Madison University * @version 1.0 */ public class ConvenienceExample { public static void main(String[] args) { double weight; int age; String address, name; JMUConsole.open(); JMUConsole.print("Enter your age: "); age = JMUConsole.readInt(); JMUConsole.print("Enter your name: "); name = JMUConsole.readLine(); JMUConsole.print("Enter your weight: "); weight = JMUConsole.readDouble(); JMUConsole.print("Enter your address: "); address = JMUConsole.readLine(); JMUConsole.printf("%s lives at %s is %d years old and weighs %5.1f", name, address, age, weight); JMUConsole.close(); } }