/********************************************************************************
 Author:           Jim Skrentny, skrentny@cs.wisc.edu
                   copyright 1999, all rights reserved
 Revised by:       Deb Deppeler, deppeler@cs.wisc.edu (2005)
                   Beck Hasti, hasti@cs.wisc.edu (2008)
 Version:          2.0
 Compiler:         JDK 1.5
 Platform:         Windows 2000
********************************************************************************/

// This import is required when doing console input.
import java.util.Scanner;

/**
 * This program demonstrates how to do console input and output in Java.
 * Java console I/O can be complicated to understand, but you can get started
 * by following and copying the code examples below.
 *
 * BUGS: none known
 */
public class ConsoleIO {

    // Data is input using a Scanner object that is connect to System.in,
    // an InputStream object, that is predefined in the java.lang package.
    private static Scanner scanner = new Scanner( System.in );

    public static void main ( String [] args ) {
    
        // Java console output is done using System.out, a PrintStream object,
        // that is predefined in the java.lang package.  Two common methods
        // are println and print, which take a string argument and display
        // it in the console window.  println causes output to continue on
        // the next line whereas print doesn't.
        System.out.print("This example " + "program ");
        System.out.println("demonstrates:");
        System.out.println("CONSOLE INPUT & OUTPUT\n\n");
    
        // With our object scanner, we can read a line of input as a String
        // using the nextLine method:
        System.out.print("Enter a string: ");
        String s = scanner.nextLine();
        System.out.println("You've entered: " + s);
        
        // If you only wanted the first character of the input, use:
        char c = s.charAt(0);
        System.out.println("The first character is: " + c);
        
        System.out.println("\n\n");
        
        // Reading numbers requires the input string to be converted to a
        // value that is assigment compatible with the primitive variable.
		// Use the Scanner classes methods to read the type of value you need.
        System.out.print("Enter an integer of 3 digits: ");
        int i = scanner.nextInt();
        System.out.println("You've entered: " + i);
        
        System.out.print("Enter a long integer of 11 digits: ");
        long l = scanner.nextLong();
        System.out.println("You've entered: " + l);
        
        System.out.print("Enter a float: ");
        float f =  scanner.nextFloat();
        System.out.println("You've entered: " + f);
        
        System.out.print("Enter a double: ");
        double d = scanner.nextDouble();
        System.out.println("You've entered: " + d);
        
        // The code below follows the parse method approach.
        System.out.print("Enter an integer of 3 digits: ");
        i = Integer.parseInt(scanner.nextLine());
        System.out.println("You've entered: " + i);
        
        System.out.print("Enter a long integer of 11 digits: ");
        l = Long.parseLong(scanner.nextLine());
        System.out.println("You've entered: " + l);
        
        System.out.print("Enter a float: ");
        f = Float.parseFloat(scanner.nextLine());
        System.out.println("You've entered: " + f);
        
        System.out.print("Enter a double: ");
        d = Double.parseDouble(scanner.nextLine());
        System.out.println("You've entered: " + d);
        
        // This is how you can read a string from the console and convert it
        // into the desired type of data.  By copying the code examples above,
        // you can now use console input and output in your programs.
    }
    
}
