slide1 l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Java Input PowerPoint Presentation
Download Presentation
Java Input

Loading in 2 Seconds...

play fullscreen
1 / 15

Java Input - PowerPoint PPT Presentation


  • 224 Views
  • Uploaded on

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;

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Java Input' - jaden


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
slide1

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

slide2

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

slide3

Scanner Creation

reference variable

Scanner keyboard = new Scanner(System.in);

object instantiation

GCOC – A.P. Computer Science

slide4

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

slide5

Scanner

Methods

GCOC – A.P. Computer Science

slide7

Reading in integers

Scanner keyboard = new Scanner(System.in);

System.out.print("Enter a 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

slide8

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

slide9

Reading in Doubles

Scanner keyboard = new Scanner(System.in);

System.out.print("Enter a double :: ");

double num = keyboard.nextDouble();

GCOC – A.P. Computer Science

slide10

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

slide11

Reading in Strings

Scanner keyboard = new Scanner(System.in);

System.out.print("Enter a string :: ");

String word = keyboard.next();

GCOC – A.P. Computer Science

slide12

Reading in Lines

Scanner keyboard =

new Scanner(System.in);

System.out.print("Enter a sentence :: ");

String sentence = keyboard.nextLine();

GCOC – A.P. Computer Science

slide13

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

slide14

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

slide15

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