0

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: enter image description here

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!

  • 2
    You can't call `S.nextToken()` twice in the loop body. Each call reads another token. Rather say `String next = S.nextToken(); and then reference `next` twice. – Gene Feb 21 '20 at 06:18

2 Answers2

0

StringTokenizer.nextToken() increments the token upon each call. You should only call it once per loop, and save the value it returns in the local scope.

import java.util.StringTokenizer;

class A {

    public static void main(String[] args) {
        String str = "Monday Tuesday Wednesday";
        String newstr = "";
        StringTokenizer S = new StringTokenizer(str);
        while(S.hasMoreTokens() == true) {
            String day = S.nextToken();
            if(day.equals("Tuesday")) {
                continue;
            } else {
                newstr = newstr + (day + " ");
            }
        }
        System.out.println(newstr);
    }
}

returns Monday Wednesday

Alea Kootz
  • 913
  • 4
  • 11
  • I get my mistake now, but I still can't understand why that exception is thrown. Could you please clarify that? –  Feb 21 '20 at 06:37
  • 1
    Oh sorry, I completely get it now, S.nextToken() is being called a fourth time in the loop but there is no fourth token and hence the 'NoSuchElement' exception is thrown. I hope I understand it correct. –  Feb 21 '20 at 06:48
  • Yeah sure. So, when you enter the loop, and hit the `if` part of the `if,else` block, the value of `S.nextToken()` is Monday. but since `nextToken()` was called as part of the check, it iterates before you continues to the `else` statement. Then the else statement uses `S.nextToken()` and stores `Tuesday` in the string, and iterates the token. You then re-enter the while loop, and call `S.nextToken()` which is `Wednesday`, iterate and proceed to the `else` which calls `nextToken()` event though there is no token to take. This gives the `NoSuchElement Exception`. – Alea Kootz Feb 21 '20 at 06:49
  • Since you understand it now, please pick a solution to accept, so that the question will be marked as solved. Do be aware though, that `StringTokenizer` is considered a legacy class, and should not be used in production code. `String.split` is the usual alternative. (https://stackoverflow.com/questions/6983856/why-is-stringtokenizer-deprecated) – Alea Kootz Feb 21 '20 at 06:54
0
public class A {
    public static void main(String[] a) {
        String str = "Monday Tuesday Wednesday";
        String newstr = "";

        StringTokenizer S = new StringTokenizer(str," ");

        while (S.hasMoreTokens()) {
            String token = S.nextToken();
            if (token.equals("Tuesday")) {
                continue;
            } else
                newstr = newstr + (token + " ");
        }

        System.out.println(newstr);
    }
}

you have to take nextToken only once