/******************************************************************** * Name: ConsoleInput.java * Author: Lara Wear * Date: Jan 29, 2014 * Purpose: To demonstrate user input from the console using the * Scanner class. *********************************************************************/ import java.util.Scanner; public class ConsoleInput { public static void main(String[] args) { // much easier declaration Scanner scan = new Scanner(System.in); // scanner object double number = 0; // double String firstName = ""; // first name String sentence = ""; // multiple words /******Integer Input **************/ // prompt for input System.out.print("Enter an integer: "); // get next int number = scan.nextInt(); // output the number System.out.println(number); /******Double Input **************/ // prompt for input System.out.print("Enter a floating point number: "); // get next double number = scan.nextDouble(); // output the number System.out.println(number); /******Single Word Input **************/ // prompt for input System.out.print("Enter your first name: "); // get next word firstName = scan.next(); // next() reads up to the next white space scan.nextLine(); // clears hard return after name // output the number System.out.println(firstName); /******One line Input **************/ // prompt for input System.out.print("Enter a sentence: "); // get next line sentence = scan.nextLine(); // output the sentence System.out.println(sentence); // waits for user to type any key to exit System.out.println("Type any key + enter to end program"); scan.next(); } // main } // ConsoleInput