1 / 15

Java Input

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;

gella
Download Presentation

Java Input

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

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

  4. 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

  5. Scanner Methods GCOC – A.P. Computer Science

  6. GCOC – A.P. Computer Science

  7. 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

  8. 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

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

  10. 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

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

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

  13. 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

  14. 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

  15. 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

More Related