how to use Scanner class in collections in java
Previous | Home | Next |
package r4r.co.in;
import java.util.*;
public class CollectionsExample {
/**
* @param args
*/
public static void main(String[] args) throws IllegalArgumentException, NoSuchElementException
{
// TODO Auto-generated method stub
/* The following code takes a string input and removes the word
* which have been used as a Delimiter
*/
try
{
String input = "tutorials java 7 java software java r4r"; // this is input string
Scanner scannerobj = new Scanner(input).useDelimiter("\\s*java\\s*"); // we have passed the string to the Scanner constructor and set java as Delimiter
System.out.println(scannerobj.next()); // sees the next token is string
System.out.println(scannerobj.nextInt()); // sees whether next token is Integer
//System.out.println(s.nextInt());
System.out.println(scannerobj.next());
System.out.println(scannerobj.next());
scannerobj.close(); // we have close the scanner object
}
catch(IllegalArgumentException e)
{
e.printStackTrace();
}
catch(NoSuchElementException e)
{
e.printStackTrace();
}
}
}
The output of the above example is as follows:
tutorials
7
software
r4r
Previous | Home | Next |