Constructor | Description | Scanner(File f) throws FileNotFoundException | This constructor allows Scanner class to read from a file at the disk. |
---|---|
Scanner(String s) | Allows a Scanner class to read from a String. |
Scanner(InputStream is) | Allows a Scanner class to read user input from user keyboard. |
Scanner(Readable fr) | Allows a Scanner class to read a Readable object as a source for input. |
Methods | Description | boolean hasNext() | This method returns true if the next token of value is available to read. |
---|---|
boolean hasNextInt() | This method returns true if the next token is of type float. |
boolean hasNextFloat() | This method returns true if the next token is of type float. |
boolean hasNextDouble() | This method returns true if the next token is of type boolean. |
boolean hasNextShort() | This method returns true if the next token is of type short. |
boolean hasNextByte() | This method returns true if the next token is of type byte. |
int nextInt() | This method returns the next token as an int type of value. |
byte nextByte() | This method returns the next token as an byte type of value. |
short nextShort() | This method returns the next token as an short type of value. |
float nextFloat() | This method returns the next token as a float type of value. |
double nextDouble() | This method returns the next token as an double type of value. |
String next() | This method returns the next token as an String type of value. |
//program when scanner class reads through the keyboard.
import java. util.*;
class A
{
public static void main(String... ar)
{
Scanner s = new Scanner(System.in);
while(s.hasNext()) //checks if there is a token value available to be read
{
if(s.hasNextInt()) //checks if next token value is of type int
System.out.println("Int is " + s.nextInt());
else if(s.hasNextDouble()) //checks if next token value is of type double
System.out.println("Double is "+s.nextDouble());
else if(s.hasNextBoolean()) //checks if next token value is of type boolean
System.out.println("Boolean is "+s.nextBoolean());
else
{
if(s.next().equals("exit")) //checks if user has entered a String value, exit
break; //if user has entered exit, control breaks out of while loop
else
System.out.println("String "+ s.next()); //prints the token value of type String.
}
}
}
}
Output
19
Int is 19
21
Int is 21
23.4
Double is 23.4
true
Boolean is true
false
Boolean is false
Hello
Bonjour!
String Bonjour!
exit
In the last code, we have read the user inputs from the command line using Scanner class.
In the preceding code, we are reading LIVE user inputs from the keyboard and as soon as user enters the first token, an int value of 19 -
import java.util.*;
import java.io.*;
class A
{
public static void main(String... ar)
{
File file = new File("D://File.txt");
try
{
FileWriter f= new FileWriter(file); //Creating a file
f.write("20 hello true 15.2 Michael false 34"); //Writing to the file
f.close();
Scanner s = new Scanner(file); //Reading the file using Scanner class.
while(s.hasNext())
{
if(s.hasNextInt())
System.out.println("Int is "+ s.nextInt());
else if(s.hasNextFloat())
System.out.println("Int is "+ s.nextFloat());
else if(s.hasNextBoolean())
System.out.println("Int is "+ s.nextBoolean());
else
System.out.println("String is "+ s.next());
} //while loop ends
} //try block ends
catch(Exception e)
{
System.out.println("Exception : "+ e);
} //catch block ends
}
}
Output -
Int is 20
String is hello
Int is true
Int is 15.2
String is Michael
Int is false
Int is 34
A try-catch block was required in this program, as we are creating a new File and then we are also calling the constructor of Scanner class that may return a FileNotFoundException.
import java.util.*;
class A
{
public static void main(String...ar)
{
String str= "1 truth is 99.9 true rest false";
Scanner s = new Scanner(str);
while(s.hasNext())
{
if(s.hasNextInt())
{
System.out.println("Int is "+ s.nextInt());
}
else if(s.hasNextFloat())
{
System.out.println("Float is "+ s.nextFloat());
}
else if(s.hasNextBoolean())
{
System.out.println("Boolean is "+s.nextBoolean());
}
else
{
System.out.println("String is "+ s.next());
}
} //while loop ends here.
}
}
Output -
D:\Java>java A
Int is 1
String is truth
String is is
Float is 99.9
Boolean is true
String is rest
Boolean is false
//Exception while reading through the Scanner class.
import java. util.*;
class A
{
public static void main(String... ar)
{
Scanner s = new Scanner(System.in); //reading user inputs from the keyboard.
while(s.hasNext())
{
if(s.hasNextInt())
{
System.out.println("Int is " + s.nextBoolean()); //trying to convert an int value to a boolean value
}
else if(s.hasNext())
System.out.println("String "+ s.next());
}
}
}
Output -
12
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextBoolean(Unknown Source)
at A.main(Scanner6.java:16)
In the preceding code, we are reading LIVE user inputs from the keyboard and as soon as user entered an int value of 12.
Coming Next
-
JSP & Servlets