- Forward


Basic Input and Output in Java
Using the JMUConsole Class


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

A Pedagogical Issue
Back SMYC Forward
  • A Common Need:
    • To accept input from the user and provide output to the user
  • Input/Output in Java:
    • Input and output in Java are object-oriented
  • The Issue:
    • You don't know about objects
How We'll Deal With This
Back SMYC Forward
  • Now:
    • We'll use a class that is not part of the Java library named JMUConsole (you'll have to download the .class file into the directory containing your source code)
  • Later:
    • We'll use standard library classes/methods
Something Else Before We Start - Special Characters
Back SMYC Forward
  • The Issue:
    • The " character is used to denote a String literal. So, how can one include a " character in a String?
  • The Resolution:
    • Use the escape sequence \" (which is a single char)
  • Example:
    • "She said \"None for me, thanks.\" and left."
Special Characters (cont.)
Back SMYC Forward
  • A Resulting Issue:
    • If you want to output a \ character it too must be escaped (i.e., \\)
  • A Related Issue:
    • Some characters (like tab, carriage return, and line feed) that can be printed can't be included in output strings in the obvious way because pressing the corresponding key causes an action (e.g., Tab) or because there isn't a corresponding key (e.g., the end-of-file character)
  • Examples:
    • \n for newline
    • \r for carriage return
    • \t for tab
One More Thing Before We Start - String Concatenation
Back SMYC Forward
  • What We've Already Discussed:
    • Declaring String variables
    • String literals
    • Assigining a String literal to a String variable
  • A Convenient Operator:
    • The binary + can be used to concatenate its two operands (one of which must be a String) and evaluates to a String
  • An Example:
    • int number; String course, department; number = 149; department = "CS"; course = department + number;
Setup/Shutdown of the JMUConsole
Back SMYC Forward
  • Setup:
    • You must call JMUConsole.open() exactly once, before you can use the JMUConsole for input or output
  • Shutdown:
    • You should call JMUConsole.close() after you are done (i.e., just before the program terminates)
Important Methods for Output
Back SMYC Forward
  • Unformatted Output:
    • JMUConsole.print(value) prints the value (whatever type it is)
    • JMUConsole.println(value) prints the value followed by a line separator
  • Formatted Output:
    • JMUConsole.printf(Format, value[, value]...) prints one or more values formatted according to the Format string
The Format String
Back SMYC Forward
  • Components:
    • String literals
    • Format specifiers
  • Format Specifiers:
    • Syntax: %[flags][width][.precision]conversion
  • Conversions:
    • 'b' for a boolean
    • 'c' for a char
    • 'd' for an integer
    • 'e' for a real number in scientific notation
    • 'f' for a real number
    • 's' for a String
  • Flags:
    • '-' for left-justified
    • '+' to always include the sign
    • ' ' to use a space for the sign of a positive number
    • '0' to pad with zeros
    • ',' to use grouping separators
    • '(' to put negative numbers in parentheses
An Example of Output
Back SMYC Forward
javaexamples/jmuconsole/PrintfExample.java
 
Important Methods for Input
Back SMYC Forward
  • nextDouble(), nextInt()
    • Return the next double value or int value
  • nextLine()
    • Returns the next line as a String (i.e., what the user has typed up to the next line delimiter)
  • next()
    • Finds and returns the next token as a String (i.e., what the user has typed up to the next token delimiter)
An Example with (output and) Input
Back SMYC Forward
javaexamples/jmuconsole/InputExample.java
 
What About the Textbook?
Back SMYC Forward
  • Output:
    • The textbook talks about 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
  • Input:
    • The textbook talks about a 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 Inconvenience
Back SMYC Forward
javaexamples/jmuconsole/InconvenienceExample.java
 
Convenience Methods for Input
Back SMYC Forward
  • readDouble(), readInt()
    • Return the next double value or int value and "consume" the newline character
  • readLine()
    • Works just like nextLine()
An Example of the Convenience Methods
Back SMYC Forward
javaexamples/jmuconsole/ConvenienceExample.java
 
There's Always More to Learn
Back -