• Like
  • Save

Loading…

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

Like this presentation? Why not share!

Scanner classes

on

  • 1,531 views

 

Statistics

Views

Total Views
1,531
Views on SlideShare
1,531
Embed Views
0

Actions

Likes
0
Downloads
48
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Scanner classes Scanner classes Presentation Transcript

    • 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
    • Importing import java.util.Scanner;
    • Creating scanner object Scanner sc = new Scanner(System.in);
    • Methods
      • nextInt():
      • receives the next token from scanner object which can be expressed as an integer and stored in integer type
      • nextFloat():
      • receives the next token from scanner object which can be expressed as an floating and stored in float type
      • nextLong():
      • receives the next token from scanner object which can be expressed as an long and stored in long type
      • nextDouble():
      • receives the next token from scanner object which can be expressed as an double and stored in double type
      • next():
      • receives the next token from scanner object as a string
      • next (): receives the next token from scanner object as a string
      • nextLine(): receives the next line of the string
      • Ex:
      • int n= in.nextInt();
      • float f = in.nextFloat();
      • String s1=in.next();
      • String s2=in.nextLine();
      • 19 52.360 Understanding computer Applications
      • Outputs:
      • 68
      • 52.360
      • Understanding
      • Computer Applications
    • Program
      • import 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);
      • }
      • }
    • Output
      • Enter int,double,a line
      • 23
      • 2345.6789
      • I Love India
      • The outputs are
      • 23
      • 2345.6789
      • I
      • Love India
    • Token checking methods
      • Boolean hasNextInt()
      • Returns true if the next token in the scanner object can be interrupted as an int value
      • Boolean hasNextLong()
      • Returns true if the next token in the scanner object can be interrupted as a long value
      • Boolean hasNextFloat()
      • Returns true if the next token in the scanner object can be interrupted as a float value
      • Boolean hasNextDouble()
      • Returns true if the next token in the scanner object can be interrupted as a double value
      • Boolean hasNext()
      • Returns true if the scanner object has another token in its input otherwise false
      • Boolean hasNextLine()
      • Returns true if the scanner object has another line in its input otherwise false