Chapter2PPTb - Chapter 2 JAVA FUNDAMENTALS CONT'D 1 READING...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Chapter 2 JAVA FUNDAMENTALS CONT’D
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 READING INPUT FROM THE KEYBOARD The Scanner Class The standard input device is normally the computer keyboard. The Java API has an object System.in which is associated with the standard input device. We will use this object in conjunction with the Scanner class to read input data from the keyboard. The Scanner class has methods that can be used to read input and format it as primitive data values or strings.
Background image of page 2
3 READING INPUT FROM THE KEYBOARD The Scanner Class To use the Scanner class in our program we must put the following statement near the top of our file, before any class definition : import java.util.Scanner; This statement tells the compiler where to find the Scanner class.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 READING INPUT FROM THE KEYBOARD The Scanner Class You must also create a Scanner object and connect it to the System.in object. You can do this with a statement like the following: Scanner keyboard = new Scanner(System.in); Create this object inside your main method before you attempt to read anything from the keyboard.
Background image of page 4
5 READING INPUT FROM THE KEYBOARD The Scanner Class The words Scanner keyboard declare a variable named keyboard with data type Scanner . This variable will reference an object of the Scanner class. Scanner keyboard = new Scanner(System.in); You could have chosen any name you wanted for the variable, but keyboard is a good one since you are going to use it to access the keyboard.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 READING INPUT FROM THE KEYBOARD The Scanner Class Scanner keyboard = new Scanner(System.in) ; The word, new , is a Java key word. The new key word is used to create an object in memory. The type of object is listed after the key word new , this is Scanner . So we are creating an object of the Scanner class. Inside the parentheses, we have System.in . Here we are saying that we want the object we are creating to be connected with the System.in object, which again is associated with the keyboard. We are assigning the address of the object created to our variable named keyboard , so keyboard will reference the object we have linked with the actual keyboard.
Background image of page 6
7 READING INPUT FROM THE KEYBOARD The Scanner Class Every object created from the Scanner class has methods that read a string of characters entered at the keyboard, converts them to a specified type, and returns this value to be stored in a variable of compatible type.
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 READING INPUT FROM THE KEYBOARD The Scanner Class For example, the code below could be used to read an integer entered at the keyboard and store it in an integer variable named age . int age; System.out.print(“Enter your age: “); age = keyboard.nextInt( );
Background image of page 8
READING INPUT FROM THE KEYBOARD The Scanner Class Make sure you get storage for the value entered by the user (declare the variable) before reading the value entered at the keyboard using the Scanner object. int age;
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/14/2008 for the course CS 1336 taught by Professor Thompson during the Spring '08 term at University of Texas at Dallas, Richardson.

Page1 / 121

Chapter2PPTb - Chapter 2 JAVA FUNDAMENTALS CONT'D 1 READING...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online