150 likes | 312 Views
Java Input. Input is NOT tested on the AP exam, but if we want to do any labs, we need input!!. Scanner Imports. import java.util.Scanner;
E N D
Java Input Input is NOT tested on the AP exam, but if we want to do any labs, we need input!! GCOC – A.P. Computer Science
Scanner Imports import java.util.Scanner; The Scanner class provides a way to read and parse input. It is often used to read input from the keyboard. Keymethods are next and hasNext. Parse - to separate (a sentence or word) into parts GCOC – A.P. Computer Science
Scanner Creation reference variable Scanner keyboard = new Scanner(System.in); object instantiation GCOC – A.P. Computer Science
Input Prompts Anytime you want user input, you must clearly state to the user what you want. System.out.println("Enter an int number : "); System.out.println("Enter a real number : "); GCOC – A.P. Computer Science
Scanner Methods GCOC – A.P. Computer Science
Reading in integers Scanner keyboard = new Scanner(System.in); System.out.print("Enter an integer :: "); int num = keyboard.nextInt(); Type in the appropriate import statement (see previous slides!) in the Interactions pane of Dr. Java and then type in the code above. GCOC – A.P. Computer Science
Scanner Example //scanner int example import java.util.Scanner; public class ScannerInts { public static void main(String args[]) { Scanner keyboard = new Scanner(System.in); System.out.print("Enter an short number(-33768 to 32767) :: "); int shorty = keyboard.nextInt(); System.out.println(shorty); System.out.print("Enter an int number (-2billion to 2billion):: "); int inty = keyboard.nextInt(); System.out.println(inty); } } Type this programin Dr. Java and make sure you understand the output. GCOC – A.P. Computer Science
Reading in Doubles Scanner keyboard = new Scanner(System.in); System.out.print("Enter a double :: "); double num = keyboard.nextDouble(); GCOC – A.P. Computer Science
Scanner Example //scanner real example import java.util.Scanner; public class ScannerReals { public static void main(String args[]) { Scanner keyboard = new Scanner(System.in); System.out.print("Enter a float :: "); float f = keyboard.nextFloat(); System.out.println(f); System.out.print("Enter a double number :: "); double d = keyboard.nextDouble(); System.out.println(d); } } Type this programin Dr. Java and make sure you understand the output. GCOC – A.P. Computer Science
Reading in Strings Scanner keyboard = new Scanner(System.in); System.out.print("Enter a string :: "); String word = keyboard.next(); GCOC – A.P. Computer Science
Reading in Lines Scanner keyboard = new Scanner(System.in); System.out.print("Enter a sentence :: "); String sentence = keyboard.nextLine(); GCOC – A.P. Computer Science
Scanner Example //scanner string example import java.util.Scanner; public class ScannerStrings { public static void main(String args[ ]) { Scanner keyboard = new Scanner(System.in); System.out.print("Enter a multi-word sentence :: "); String sentence = keyboard.nextLine(); System.out.println(sentence); System.out.print("Enter a one word string :: "); String s = keyboard.next(); System.out.println(s); } } Type this programin Dr. Java and make sure you understand the output. GCOC – A.P. Computer Science
Multiple Inputs Scanner keyboard = new Scanner(System.in); System.out.println(keyboard.nextInt()); System.out.println(keyboard.nextInt()); System.out.println(keyboard.nextInt()); OUTPUT 123 INPUT1 2 3 4 5 GCOC – A.P. Computer Science
Scanner Example //scanner string example import java.util.Scanner; public class ScannerStrings { public static void main(String args[]) { Scanner keyboard = new Scanner(System.in); System.out.print("Enter a multi-word sentence :: "); String sentence = keyboard.nextLine(); System.out.println(sentence); System.out.print("Enter a one word string :: "); String s = keyboard.next(); System.out.println(s); } } GCOC – A.P. Computer Science