source file name:  strtokendemo.java

import java.util.*;

import java.text.*;

class strtokendemo

{

public static void main(String as[])

{

System.out.println("String Tokenizer class program");

String s="123.50 2.5 l6.3f 520 10.4 10l.25f 10.2505";

System.out.println(" list of tokens: ");

StringTokenizer st=new StringTokenizer(s);

double sum=0.0;

int cnt=0;

 while(st.hasMoreTokens())

{

String str=st.nextToken();

try

{

double n=Double.parseDouble(str);

sum=sum+n;

System.out.print(str+" ");

}

catch(NumberFormatException e)

{cnt++;}


}

DecimalFormat df=new DecimalFormat();

df.setMinimumFractionDigits(4);

System.out.println("\n sum of valid numbers=  "+df.format(sum));

System.out.println(" given string contains " + cnt + " illegal data ");

}

}

output:


D:\JavaPrograms\Misc>javac strtokendemo.java


D:\JavaPrograms\Misc>java strtokendemo

String Tokenizer class program

 list of tokens:

123.50 2.5 520 10.4 10.2505

 sum of valid numbers=  666.6505

 given string contains 2 illegal data