11
Java Input Prepared by: Jean Michael Castor

Java input

Embed Size (px)

Citation preview

Page 1: Java input

Java Input

Prepared by:Jean Michael Castor

Page 2: Java input

Introduction

• For some unfathomable reason, Java has never made it very easy to read data typed in by the user of a program.

Page 3: Java input

Introduction

• You've already seen that output can be displayed to the user using the subroutine System.out.print. This subroutine is part of a pre-defined object called System.out. The purpose of this object is precisely to display output to the user.

Page 4: Java input

• There is also a corresponding object called System.in that exists to read data input by the user, but it provides only very primitive input facilities, and it requires some advanced Java programming skills to use it effectively.

• To use System.in, a Java Scanner is used that will get the inputs.

Page 5: Java input

Scanner Creation/DeclarationScanner keyboard = new Scanner(System.in);

• Scanner is a class which must be instantiated before it can be used. In other words, you must make a new Scanner if you want to use Scanner. A reference must be used to store the location in memory of the Scanner object created.

• System.in is the parameter passed to the Scanner constructor so that Java will know to connect the new Scanner to the keyboard. keyboard is a reference that will store the location of newly created Scanner object.

Reference variable Object Instantiation

Page 6: Java input

Scanner Imports

• In order to use Scanner, you must import java.util.Scanner.

import java.util.Scanner;

Page 7: Java input

Scanner MethodsNAME USEnextInt(); Returns the next integer value

nextDouble(); Returns the next double value

nextFloat(); Returns the next float value

nextLong(); Returns the next long value

nextShort(); Returns the next short value

next(); Returns the next one word String value

nextLine(); Returns the next multiple word String value

Page 8: Java input

Reading in Integers

Scanner keyboard = new Scanner(System.in);System.out.print(“ Enter Value”);int num = keyboard.nextInt();

• The nextInt() method is used to tell a Scanner object to retrieve the next integer value entered.

• In the example, the next integer typed in on the keyboard would be read in and placed in the integer variable num.

• nextInt() will read up to the first whitespace value entered.

Page 9: Java input

Integers

int num = keyboard.nextInt();

• The nextInt() method will read in the next integer. If a non-integer value is encountered such as a decimal value, the result will be run-time exception.

• keyboard is a reference that refers to a Scanner object.

method callreference variabledata type

identifier

Page 10: Java input

Strings

String word= keyboard.nextLine();

• The nextLine() method will read in the next text value entered. A numeric or non-numeric text value will be accepted.

• In the example, the next text entered on the keyboard would be read in and placed in variable word.

• The nextLine() method would read up to the first whitespace encountered. Whitespace would be any space, any tab, or any enter key.

method callreference variabledata type

identifier

Page 11: Java input

Prompts

System.out.print(“Enter an integer: ”);

• When performing input operations, it is a must to use prompts. A prompt is a way of indicating to a user what type of data to enter.

• The prompt above indicates that an integer value is expected.