Scanner class in Java
Scanner class is added in J2SE5 .This is used to read formatted input and convert into binary form.
Why scanner ?
We know that it is possible to read formatted input from file or console. But it requires lot of programing effort. Scanner simplifies this.
Scanner constructors
Scanner can be created for different input source like String,InputStream or any object that implements Readable or ReadableByteChannel.
1 2 |
FileReader fr= new FileReader("sample.txt"); Scanner scanner=new Scanner(fr); |
This works because FileReader implements Readable interface.
1 |
Scanner scanner=new Scanner(System.in); |
This works because System.in an object of InputStream.
Scanner can read String also as below
1 2 |
String str="This is from j2eereference.com"; Scanner scanner= new Scanner(str); |
Scanner provide two types of methods to read input . hasNextX and nextX , where X is the type of the data., like HasNextInt,hasNextDouble. next method will throw NoSuchElementException if next method cannot find the type of data it is looking for.
Now lets see some example code to read different types of inputs.
Reading String using scanner :
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { String str = "This is from j2eereference.com"; Scanner scanner = new Scanner(str); while (scanner.hasNext()) { System.out.println("Item :" + scanner.next()); } } } |
output
Item :This
Item :is
Item :from
Item :j2eereference.com
Reading int values using Scanner
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { int item; String str = "10 20 30 40"; Scanner scanner = new Scanner(str); while (scanner.hasNextInt()) { item=scanner.nextInt(); System.out.println("Item :" +item); } } } |
Output
Item :10
Item :20
Item :30
Item :40
Reading data from file using Scanner
Here in this example we will create a file called sample.txt and write different types of data into it. In this scenario we will have to check the type of the data while reading.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) throws IOException { FileWriter fout = new FileWriter("sample.txt"); fout.write("j2eereference.com true 10.5"); fout.close(); FileInputStream fin = new FileInputStream("sample.txt"); Scanner scanner = new Scanner(fin); while (scanner.hasNext()) { if (scanner.hasNextBoolean()) System.out.println("Boolean : " + scanner.nextBoolean()); if (scanner.hasNextDouble()) System.out.println("Double : " + scanner.nextDouble()); if (scanner.hasNext()) System.out.println("String : " + scanner.next()); } fin.close(); } } |
Output
String : j2eereference.com
Boolean : true
Double : 10.5
Setting Delimiter:
We can define where the token starts and ends with the following method
Scanner useDelimiter(String pattern);
Scanner useDelimiter(Pattern pattern); // Here pattern is the regular expression that specify the delimiter set
Now lets see an example using delimiter
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { String str = "10#20#30#40"; Scanner scanner = new Scanner(str); scanner.useDelimiter("#"); while (scanner.hasNextInt()) { System.out.println("Item :" + scanner.nextInt()); } } } |
Output
Item :10
Item :20
Item :30
Item :40
Leave a Reply