Vous êtes sur la page 1sur 6

Java Scanner Class

return Method Description


void close() Closes the scanner object.
Returns the Pattern the Scanner object is
Pattern delimiter()
currently using to match delimiters.
This method returns a String object that
String findInLine(Pattern pattern) satisfies the Pattern object specified as method
argument.
Attempts to find the next occurrence of a
String findInLine(String pattern) pattern constructed from the specified string,
ignoring delimiters.
findWithinHorizon(Pattern Attempts to find the next occurrence of the
String
pattern, int horizon) specified pattern.
findWithinHorizon(String This method simply attempts to find the next
String
pattern, int horizon) occurrence of a pattern input ignoring delimiter
Returns true if this scanner has another token in
boolean hasNext()
its input.
Returns true if the next complete token matches
boolean hasNext(Pattern pattern)
the specified pattern.
Returns true if the next token matches the
boolean hasNext(String pattern)
pattern constructed from the specified string.
Returns true if the next token in this scanner's
boolean hasNextBigDecimal() input can be interpreted as a BigDecimal using
the nextBigDecimal() method.
Returns true if the next token in this scanner's
boolean hasNextBigInteger() input can be interpreted as a BigInteger in the
default radix using the nextBigInteger() method.
Returns true if the next token in this scanner's
input can be interpreted as a BigInteger in the
boolean hasNextBigInteger(int radix)
specified radix using the nextBigInteger()
method.
This method checks if the Scanner object has
boolean hasNextBoolean()
boolean data type on its buffer.
This method returns true if the next byte on the
boolean hasNextByte() scanner buffer can be translated to byte data
type otherwise false.
Returns true if the next token in this scanner's
boolean hasNextByte(int radix) input can be interpreted as a byte value in the
specified radix using the nextByte() method.
Returns true if the next token in this scanner's
boolean hasNextDouble() input can be interpreted as a double value using
the nextDouble() method.
Returns true if the next token in this scanner's
boolean hasNextFloat() input can be interpreted as a float value using
the nextFloat() method.
boolean hasNextInt() Returns true if the next token in this scanner's
Java Scanner Class
return Method Description
input can be interpreted as an int value in the
default radix using the nextInt() method.
This method returns boolean, true if the token
can be interpreted as int data type with respect
boolean hasNextInt(int radix)
to the radix used by the scanner object
otherwise false.
This method returns a boolean data type which
boolean hasNextLine() corresponds to the existence of new line on the
String tokens which the Scanner object holds.
Returns true if the next token in this scanner's
boolean hasNextLong() input can be interpreted as a long value in the
default radix using the nextLong() method.
Returns true if the next token in this scanner's
boolean hasNextLong(int radix) input can be interpreted as a long value in the
specified radix using the nextLong() method.
Returns true if the next token in this scanner's
boolean hasNextShort() input can be interpreted as a short value in the
default radix using the nextShort() method.
This method returns boolean, true if the token
can be interpreted as short data type with
boolean hasNextShort(int radix)
respect to the radix used by the scanner object
otherwise false.
Returns the IOException last thrown by this
IOException ioException()
Scanner's underlying Readable.
This method returns a Locale which the
Locale locale()
Scanner class is using.
This method returns a MatchResult object
MatchResul
match() which corresponds to the result of the last
t
operation by the scanner object.
Finds and returns the next complete token from
String next()
this scanner.
Returns the next token if it matches the
String next(Pattern pattern)
specified pattern.
Returns the next token if it matches the pattern
String next(String pattern)
constructed from the specified string.
Scans the next token of the input as a
BigDecimal nextBigDecimal()
BigDecimal.
Scans the next token of the input as a
BigInteger nextBigInteger()
BigInteger.
Scans the next token of the input as a
BigInteger nextBigInteger(int radix)
BigInteger.
Scans the next token of the input into a boolean
boolean nextBoolean()
value and returns that value.
byte nextByte() Scans the next token of the input as a byte.
Java Scanner Class
return Method Description
byte nextByte(int radix) Scans the next token of the input as a byte.
double nextDouble() Scans the next token of the input as a double.
float nextFloat() Scans the next token of the input as a float.
int nextInt() Scans the next token of the input as an int.
int nextInt(int radix) Scans the next token of the input as an int.
Advances this scanner past the current line and
String nextLine()
returns the input that was skipped.
long nextLong() Scans the next token of the input as a long.
long nextLong(int radix) Scans the next token of the input as a long.
short nextShort() Scans the next token of the input as a short.
short nextShort(int radix) Scans the next token of the input as a short.
int radix() Returns this scanner's default radix.
The remove operation is not supported by this
void remove()
implementation of Iterator.
Scanner reset() Resets this scanner.
Skips input that matches the specified pattern,
Scanner skip(Pattern pattern)
ignoring delimiters.
Skips input that matches a pattern constructed
Scanner skip(String pattern)
from the specified string.
Returns the string representation of this
String toString()
Scanner.
Sets this scanner's delimiting pattern to the
Scanner useDelimiter(Pattern pattern)
specified pattern.
Sets this scanner's delimiting pattern to a
Scanner useDelimiter(String pattern)
pattern constructed from the specified String.
Scanner useLocale(Locale locale) Sets this scanner's locale to the specified locale.
Sets this scanner's default radix to the specified
Scanner useRadix(int radix)
radix.

Java Scanner Example to get input from console


1. import java.util.Scanner;
class ScannerTest{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);

System.out.println("Enter your rollno");


int rollno=sc.nextInt();
System.out.println("Enter your name");
String name=sc.next();
Java Scanner Class
System.out.println("Enter your fee");
double fee=sc.nextDouble();
System.out.println("Rollno:"+rollno+" name:"+name+" fee:"+fee);
sc.close();
}
}

Java Scanner Example with delimiter


2. import java.util.*;
public class ScannerTest2{
public static void main(String args[]){
String input = "10 tea 20 coffee 30 tea buiscuits";
Scanner s = new Scanner(input).useDelimiter("\\s");
System.out.println(s.nextInt());
System.out.println(s.next());
System.out.println(s.nextInt());
System.out.println(s.next());
s.close();
}}

Significance of System.out.println() in java

public final class System


extends Object

The System class contains several useful class fields and methods. It cannot be instantiated.
Among the facilities provided by the System class are standard input, standard output, and
error output streams; access to externally defined properties and environment variables; a
means of loading files and libraries; and a utility method for quickly copying a portion of an
array.

public static final PrintStream out


The "standard" output stream. This stream is already open and ready to accept output data.
Typically this stream corresponds to display output or another output destination specified
by the host environment or user.
For simple stand-alone Java applications, a typical way to write a line of output data is:

System.out.println(data)

Inside the System class is the declaration of out that looks like: public static final
PrintStream out, and inside the Prinstream class is a declaration of println() that has a
method signature that looks like: public void println().

Here is what the different pieces of System.out.println() actually look like:


Java Scanner Class

//the System class belongs to java.lang package


class System {
public static final PrintStream out;
//...
}

//the Prinstream class belongs to java.io package


class PrintStream{
public void println();
//...
}

System is a final class in java.lang package. As per javadoc, Among the facilities
provided by the System class are standard input, standard output, and error output
streams; access to externally defined properties and environment variables; a means of
loading files and libraries; and a utility method for quickly copying a portion of
an array
out is a static member field of System class and is of type PrintStream. Its access
specifiers are public final. This gets instantiated during startup and gets mapped with
standard output console of the host. This stream is open by itself immediately after its
instantiation and ready to accept data.
println is a method of PrintStream class. println prints the argument passed to the
standard console and a newline. There are multiple println methods with different
arguments (overloading). Every println makes a call to print method and adds a
newline. printcalls write() and the story goes on like that.
Java Scanner Class

Vous aimerez peut-être aussi