In this program, I have a String with words separated by spaces and I want to remove a particular word and then print the new String. This is my code below:
import java.util.*;
class A
{
public static void main()
{
String str="Monday Tuesday Wednesday";
String newstr="";
StringTokenizer S=new StringTokenizer(str);
while(S.hasMoreTokens()==true)
{
if(S.nextToken().equals("Tuesday"))
{
continue;
}
else
newstr=newstr+(S.nextToken()+" ");
}
System.out.println(newstr);
}
}
In the above code, I want to delete the word 'Tuesday' from the String, and print the new String. But when I run the program, the following exception is thrown:
When I want to remove the word 'Wednesday' after changing the code, only 'Tuesday' is shown on Output screen. Likewise, when I want to remove 'Monday', only 'Wednesday' is shown as output.
I would really like some help on this so that I that I can understand the 'StringTokenizer' class better.
Thank You!