0

Hi all, I have a slight problem when checking if a "Token" is in my StringTokenizer string.

My code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.String.*;
import java.util.*;

public class TextAnalyser {


public static void main( String args )
{
    StringTokenizer stringTokenizer = new StringTokenizer("You are tokenizing string");
    System.out.println("The total no. of tokens generated :  " + stringTokenizer.countTokens());
    while(stringTokenizer.hasMoreTokens()){
        System.out.println(stringTokenizer.nextToken());

        if(stringTokenizer.nextToken() == "are"){
        System.out.println("Lol");
        }       
}

}

}

The output:

The total no. of tokens generated :  4
You
tokenizing

So i'm trying to recognise a certain string token to pick up and print it out. Eventually I want to take it out, however it does not display "Lol" and it also doesn't display string?

Not sure if i'm going to right way about using nextToken(), but i don't see what else i could use

Edited by tleverington1: becausea

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by NormR1
0

You should use the equals() method to compare Strings. The == operator mostly is for primitives.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.