0

I have written this method countToken that takes a Scanner s and a String t as parameters, and returns an int. It returns the number of time that t occurs as a token in s. This is as far as I can go and to me it makes sense but it doesnt work right.

public static int countToken(Scanner s, String t)
{
    int count = 0;

    while (s.hasNext()==true)
    {
        if (s.next()==t)
        {
            count++;
        }
    }
    return count;
}
Rustam
  • 6,485
  • 1
  • 25
  • 25
Gama
  • 352
  • 2
  • 16
  • 1
    Please give an example of input and expected output... Your question doesn't make sense to me. – StackFlowed Sep 15 '14 at 19:11
  • 1
    possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – David Conrad Sep 15 '14 at 19:13
  • Use `.equals()` to compare strings in Java, not `==`. – David Conrad Sep 15 '14 at 19:14
  • You should post up and expected output and an example at least – Kick Buttowski Sep 15 '14 at 19:18
  • Gamaliel, after coming back to the question after seeing you rewarded me with the answer, I realized that I made a mistake in thinking that next() will return an empty string if the Scanner matches your input of t as the last data within your Scanner's input. You can alleviate this through regular expressions matching everything but t. I'll see if I can find time later today to come back and fix the answer. – NESPowerGlove Sep 15 '14 at 21:39

2 Answers2

0

So this is a way I figured after reading all comments and suggestions. Let me know what you think

public static int countToken(Scanner s, String t)
{
    int count=0;
    String token="";
    while (s.hasNext())
    {
        token = s.next();

        if(token.equals(t))
        {
        count++;
        }
        else
        {
        }
    }
    return count;
}
Gama
  • 352
  • 2
  • 16
  • 1
    If t is a String that has no whitespace in it, and the Scanner you are passing in still has the default delimiter set, then this should work. Note that String is immutable, so when you create String token = "" outside the while statement, that is a String by itself that will never be used again. Each time you reassign a String reference to a new String, it will create another String instead of placing the new String data into the existing String object (immutable). So it would read more naturally in Java if you declared token within the while. – NESPowerGlove Sep 15 '14 at 23:40
-1

Try this :

countToken(new Scanner("The book is the give the"), "the");

here Scanner contains "The book is the give the" and token t is "the". so we have to count how many "the" occurs in the Scanner s"The book is the give the" .

public static int countToken(Scanner s, String t)
    {
        int count = 0;

        while (s.hasNext())
        {
            String word=s.next();
            if (word.toLowerCase().equals(t.toLowerCase()))
            {
                count++;
            }
        }
        return count;
    }

output : 3

Rustam
  • 6,485
  • 1
  • 25
  • 25