Token
parsing using default delimiter and user defined delimiter-
import
java.util.*;
public class ScanDL
{
public static void main(String
args[])
{
String St;
Scanner sc1;
Scanner sc=new
Scanner(System.in);
System.out.print("\tEnter a Sentence : ");
St=sc.nextLine();
sc1=new
Scanner(St);//.useDelimiter(" ");
System.out.println("\n\tDefaul
Delimiter :\n");
while(sc1.hasNext())
{
System.out.println("\t"+sc1.next());
}
sc1.close();
System.out.print("\tEnter a Sentence : ");
St=sc.nextLine();
sc1=new
Scanner(St).useDelimiter(",");
System.out.println("\n\tUser
Defined Delimiter :\n");
while(sc1.hasNext())
{
System.out.println("\t"+sc1.next().trim());
}
}
}
Output:
Enter a Sentence : Hope is a waking dream.
Defaul Delimiter :
Hope
is
a
waking
dream.
Enter
a Sentence : Hard Work, Honesty, Dedication, all matters.
User Defined Delimiter :
Hard Work
Honesty
Dedication
all matters.
Token
parsing using multiple delimiter.
import
java.util.*;
public class ScanDelim
{
public static void main(String
args[])
{
String St;
int a;
Scanner sc=new
Scanner(System.in);
System.out.print("\tEnter a
Sentence : ");
St=sc.nextLine();
Scanner sc1=new
Scanner(St).useDelimiter("\\.\\s|\\?\\s|\\!\\s");
while(sc1.hasNext())
{
System.out.println("\t"+sc1.next());
}
}
}
Output
Enter a Sentence : Hello! How are you?
Hope, all is well. I am fine too.
Hello
How are you
Hope, all is well
I am fine too.