scanner classes

19
Scanner classes

Upload: sonybabu

Post on 17-Jan-2015

1.280 views

Category:

Documents


5 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Scanner classes

Scanner classes

Page 2: Scanner classes

Allows the user to input the values of various types

Defined within a package java.util

This allows the user to input the values from

either the keyboard or from file without using any conversion

Page 3: Scanner classes

Importing

import java.util.Scanner;

Page 4: Scanner classes

Creating scanner object

Scanner sc = new Scanner(System.in);

Page 5: Scanner classes

Methods

• nextInt():

• receives the next token from scanner object which can be expressed as an integer and stored in integer type

Page 6: Scanner classes

• nextFloat():

• receives the next token from scanner object which can be expressed as an floating and stored in float type

Page 7: Scanner classes

• nextLong():

• receives the next token from scanner object which can be expressed as an long and stored in long type

Page 8: Scanner classes

• nextDouble():

• receives the next token from scanner object which can be expressed as an double and stored in double type

Page 9: Scanner classes

• next():

• receives the next token from scanner object as a string

Page 10: Scanner classes

• next (): receives the next token from scanner object as a string

• nextLine():receives the next line of the string

Page 11: Scanner classes

Ex:int n= in.nextInt();float f = in.nextFloat();String s1=in.next();String s2=in.nextLine();19 52.360 Understanding computer ApplicationsOutputs:

6852.360UnderstandingComputer Applications

Page 12: Scanner classes

Programimport java.util.Scanner;class scanner1{void main(){Scanner sc = new Scanner(System.in);int n;double d;String word;String line;System.out.println("Enter int,double,a line");n=sc.nextInt();d=sc.nextDouble();word=sc.next();line=sc.nextLine();System.out.println("The outputs are");System.out.println(n);System.out.println(d);System.out.println(word);System.out.println(line);}}

Page 13: Scanner classes

Output

Enter int,double,a line232345.6789I Love India

The outputs are232345.6789ILove India

Page 14: Scanner classes

Token checking methods

• Boolean hasNextInt()

• Returns true if the next token in the scanner object can be interrupted as an int value

Page 15: Scanner classes

• Boolean hasNextLong()

• Returns true if the next token in the scanner object can be interrupted as a long value

Page 16: Scanner classes

• Boolean hasNextFloat()

• Returns true if the next token in the scanner object can be interrupted as a float value

Page 17: Scanner classes

• Boolean hasNextDouble()

• Returns true if the next token in the scanner object can be interrupted as a double value

Page 18: Scanner classes

• Boolean hasNext()

• Returns true if the scanner object has another token in its input otherwise false

Page 19: Scanner classes

• Boolean hasNextLine()

• Returns true if the scanner object has another line in its input otherwise false