Java Scanning Basics
üA Scanner reads tokens from the
underlying source.
üA token is a portion of input that
is delineated by a set of delimiters, which is by default whitespace.
üA token is read by matching it with
a particular regular expression.
üScanner follow the procedure below:
1. Determine if a specific type of input is
available by calling one of the hasNextX methods.
2. If input is available, read it by calling
one of nextX method.
3. Repeat the process until the input is
exhausted.
Note: if nextX() method does not find a matching token,
it throws
a
NoSuchElementException.
Important
Methods of Scanner Class
public
Scanner(InputStream in) // Scanner(): convenience constructor for an InputStream object
boolean hasNext()
//Return true if another token of any type is available to be read.
boolean hasNextBoolean()
//Return true if a boolean value is available to read.
boolean hasNextByte()
//Return true if a byte value is available to read.
boolean hasNextShort() //Return true if a byte value is available
to read.
boolean hasNextInt()
//Return true if a int value is available to read.
boolean hasNextLong()
//Return true if a long value is available to read.
boolean hasNextFloat() //Return true if a float value is
available to read.
boolean hasNextDouble() //Return true if a double value is
available to read.
Important
Methods of Scanner Class
int nextInt() // return next token as int
value.
short
nextShort()
// return next token as short value.
byte nextByte() // return next token as
byte value.
long nextLong() // return next token as long
value.
double
nextDouble() // return next token as double value
float
nextFloat() // return next token as float
value
String
next() //return next token of any type
from the
input source
String
nextLine() // return the next line of input
as a
string
Taking
Input from the File
üThe following lines read data from
a file:
FileReader fin = new FileReader(“c:\\f_input.txt”);
Scanner test = new Scanner(fin);
üFileReader:
-
belongs to java.io package.
-It creates an object that can read the
content of a file.
Taking
Input from the File
import
java.io.*;
import
java.util.*;
}
class
file_input
{
public static void main(String args[]) throws IOException
{
double i;
String str;
FileReader
fin = new FileReader("c:\\f_input.txt");
Scanner test = new Scanner(fin);
while(test.hasNext())
{
if(test.hasNextDouble())
{
i=test.nextDouble();
System.out.println("The number is:" + i);
}
else
{
str
= test.next();
System.out.println(str);
}
}
fin.close();
}
}
Taking
Input from a File
Suppose
the file contains the following data:
2 3.4
5 6 7.4 9.1 10.5 done
Output:
The
number is: 2.0
The
number is: 3.4
The
number is: 5.0
The
number is: 6.0
The
number is: 7.4
The
number is: 9.1
The
number is: 10.5
done
Writing
Data into a File
import
java.io.*;
class
test
{
public static void main (String args[])
{
FileWriter fout = new FileWriter(“c:\\test1.txt”);
fout.write(“2 3.4 5 6 7.4 9.1 10.5 done”);
fout.close();
}
}
No comments:
Post a Comment