Presentation is loading. Please wait.

Presentation is loading. Please wait.

A.P. Computer Science Input is NOT tested on the AP exam, but if we want to do any labs, we need input!!

Similar presentations


Presentation on theme: "A.P. Computer Science Input is NOT tested on the AP exam, but if we want to do any labs, we need input!!"— Presentation transcript:

1 A.P. Computer Science Input is NOT tested on the AP exam, but if we want to do any labs, we need input!!

2 A.P. Computer Science 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. Key methods are next and hasNext. Parse - to separate (a sentence or word) into parts

3 A.P. Computer Science Scanner keyboard = new Scanner(System.in); reference variable object instantiation

4 A.P. Computer Science 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 : ");

5 A.P. Computer Science

6 Scanner frequently used methods NameUse nextInt()returns the next int value nextDouble()returns the next double value nextFloat()returns the next float value nextLong()returns the next long value nextByte()returns the next byte value nextShort()returns the next short value next()returns the next one word String nextLine()returns the next multi word String

7 A.P. Computer Science 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.

8 A.P. Computer Science //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 program in Dr. Java and make sure you understand the output.

9 A.P. Computer Science Scanner keyboard = new Scanner(System.in); System.out.print("Enter a double :: "); double num = keyboard.nextDouble();

10 A.P. Computer Science //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 program in Dr. Java and make sure you understand the output.

11 A.P. Computer Science Scanner keyboard = new Scanner(System.in); System.out.print("Enter a string :: "); String word = keyboard.next();

12 A.P. Computer Science Scanner keyboard = new Scanner(System.in); System.out.print("Enter a sentence :: "); String sentence = keyboard.nextLine();

13 A.P. Computer Science //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 program in Dr. Java and make sure you understand the output.

14 A.P. Computer Science Scanner keyboard = new Scanner(System.in); System.out.println(keyboard.nextInt()); OUTPUT 1 2 3 INPUT 1 2 3 4 5

15 A.P. Computer Science //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); }


Download ppt "A.P. Computer Science Input is NOT tested on the AP exam, but if we want to do any labs, we need input!!"

Similar presentations


Ads by Google