Core Java Tutorial

Introduction of Core Java

How To Install JDk and Set of Path

Syntax of java Program

Difference between Java and C/C++

Advantage and Disadvantage of Java

What is Java

Why Java is not Pure Object Oriented Language

Java has Following Features/Characteristics

Limitation of Java Language and Java Internet

Common Misconception about Java

Simple Program of Java

Integrated Development Environment in java

Compile and Run Java Program

Applet and Comments in Java

Tokens in Java

Keywords in Java

Identifier and Variables in Java

Literals/Constants

Data Type in Java

Assignments and Initialization in Java

Operators in Java

Rule of Precedence in Java

Operator on Integer and Separators in Java Programming

Java Control Flow of Statements

If and If-else Selection Statement

Nested If-else and If-else-If Selection Statement

switch case and conditional operator Selection Statement

for and while Loop

do..while and for each Loop

break and labeled break statement

continue and labeled continue statement

return Statement and exit() Method

Escape Sequence for Special Characters and Unicode Code

Constants and Block or Scope

Statement in Java

Conversions between Numeric Types in Java

Import Statement in Java

User Input in Java using Scanner Class

User Input in Java using Console Class

Array in Java

One Dimensional Array

Two Dimensional Array

Two Dimensional Array Program

Command Line Argument in Java

String args Types in Java

Uneven/Jagged array in java

Math Class Function and Constant

Math Class all Function used in a program

Enumerated Types in Java

Object Oriented Programming v/s Procedural Programming

Object Oriented Programming Concepts in Java

Introduction to Class,Object and Method in Java

Class Declaration in Java

Class & Objects in java

Encapsulation in Java

Modifiers/Visibility for a Class or Interrface or member of a Class

Polymorphism in Java

Runtime polymorphism (dynamic binding or method overriding)

how to read various tokens from a file using Scanner class in collections in java

how to read various tokens from a file using Scanner class in collections in java

Previous Home Next

 



 


 
package r4r.co.in;

import java.util.*;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class CollectionsExample {

/**
* @param args
*/
public static void main(String[] args) throws IllegalArgumentException, NoSuchElementException, IOException
{
// TODO Auto-generated method stub



try
{
int i;
String string;
boolean bool;
double dbl;

FileWriter fwrite=new FileWriter("C:\\shashi\\scannerfile.txt"); // here we have created a writable file at the "C:\\shashi\\scannerfile.txt" location
fwrite.write("Hey guys!! this is 1 true example of scanner class"); // we have write the following string to the file created
fwrite.close(); // close the writable file

FileReader fread=new FileReader("C:\\shashi\\scannerfile.txt"); // creating a FileReader object
Scanner scannerobj = new Scanner(fread); // passing the fread to Scanner class's constructor
while(scannerobj.hasNext()) // loop till scannerobj has some more tokens to read
{
if(scannerobj.hasNextInt()) // checks whether the token is an integer
{
i = scannerobj.nextInt();
System.out.println("The integer values are: "+i); // displays the integer
}

else if(scannerobj.hasNextBoolean()) // checks whether the token is a boolean value
{
bool = scannerobj.nextBoolean();
System.out.println("The boolean values are: "+bool); // displays the boolean value

}
else if(scannerobj.hasNextDouble()) // checks whether the next token is a double value
{
dbl = scannerobj.nextDouble();
System.out.println("The double values are: "+dbl); // displays the double value

}
else
{
string = scannerobj.next();
System.out.println("The string words are: "+string); // displays the string value
}
}

}

catch(IllegalArgumentException e)
{
e.printStackTrace();
}

catch(NoSuchElementException e)
{
e.printStackTrace();
}

catch(IOException e)
{
e.printStackTrace();
}
}
}


The output of the above example is given as:

The string words are: Hey
The string words are: guys!!
The string words are: this
The string words are: is
The integer values are: 1
The boolean values are: true
The string words are: example
The double values are: 4.78
The string words are: of
The string words are: scanner
The string words are: class

Previous Home Next