JMU
Basic Input and Output in Java
Using the JMUConsole Class


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


A Pedagogical Issue
How We'll Deal With This
Something Else Before We Start - Special Characters
Special Characters (cont.)
One More Thing Before We Start - String Concatenation
Setup/Shutdown of the JMUConsole
Important Methods for Output
The Format String
An Example of Output
javaexamples/jmuconsole/PrintfExample.java
/**
 * 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();
    }
}
        
Important Methods for Input
An Example with (output and) Input
javaexamples/jmuconsole/InputExample.java
/**
 * 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();
    }
}
        
What About the Textbook?
An Inconvenience
javaexamples/jmuconsole/InconvenienceExample.java
/**
 * 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();
    }
}

        
Convenience Methods for Input
An Example of the Convenience Methods
javaexamples/jmuconsole/ConvenienceExample.java
/**
 * 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();        
    }
}