Monday, 3 December 2018

Scanner class in java | Scanner class in java with example | java scanner input statement - Online help

Scanner class in java:

             A scanner class is used to enable a program to read data i.e. numbers and strings. Before using a scanner class, the program must import the package import java .util.*;.A Scanner class to create and obtain input from command window is as follows:

Scanner input=new scanner (system .in);

Methods:
The various methods of the scanner class are as follows:
1.int nextInt()
2.double nextDouble()
3.String next ()
4.string nextLine ()
5.boolean hasNext ()
6.boolean hasNextDouble

    

Methods
Description
int nextInt()
Returns an integer value. The default value for a field of type int 0.
double nextDouble()
Returns a double value. The default value for a field of type double is 0.0
String next ()
Reads individual words. i.e., The method next reads the character until a whitespace character such as space, tab or newline is encountered, then return a string. (excluding the whitespace)
string nextLine ()
Read a line of text. The methods nextLine read characters typed by the user until the newline character is encountered, then return a string(excluding the new line)
boolean hasNext ()
Determines whether there is another word in the input. The method has Next returns the Boolean value true if there is more data; otherwise, it returns false.
boolean hasNextInt
Determines whether the next character sequence represents an integer.
boolean hasNextDouble
Determine whether the next character sequence represents a floating point.

Program:
Import java .util.*;
Class Input Demo
{
Public static void main (String args [])
{
Int a, b;
double pi;
String name;
Scanner s=new Scanner (System .in);
System.out .println(“Enter the value of a & b”);
A=s .next Int ();
B=s .next Int ();
System.out.println(“Enter the value of pi”);
Pi=s .next Double();
System.out.println (“Enter the your name”);
Name=s next ();
System .out.println (“The value of a=”+a);
System.out.println (“The value of b=”+b);
System.out.println(“The value of pi=”+pi);
System.out.println(“Name=”+name);
}
}

                       If  you guys want to know about jumping statement in detail click this below link:
https://beingcomputeracy.blogspot.com/2018/12/jumping-statement-in-java-break.html

No comments:

Post a Comment