0

I want to count the number of times "the" shows up in an array of tokens I made from user input and store it in a variable named "theCount". I'm iterating through the array with a for loop and checking for "the" with an if statement.

I am not allowed to use regular expressions.

This is what I have so far:

import java.util.*;

public class theCount
{
    public static void main (String[] args)
    {
        Scanner userInput = new Scanner(System.in);

        System.out.print("Enter a sentence: ");
        String sentence = userInput.nextLine();

        String[] input =  sentence.split(" the");

        int theCount = 0;

        for (String token : input) {
            if (token == "the")
                theCount++;
                System.out.print("\n" + theCount); //I want it printed after
                                                   //iteration. 

        }




    }


}
Yazan
  • 6,074
  • 1
  • 19
  • 33
Jason_Silva
  • 127
  • 3
  • 13
  • move the `System.out.print...` out of the `for loop` bracket and the space before `the` should be removed in split – Yazan Nov 20 '16 at 07:35
  • 3
    If the `split()` results in the string being split, you already know that “the” has been found. Just print `input.length - 1` as the count of “the”. No need for a loop. – Ole V.V. Nov 20 '16 at 07:42

4 Answers4

1

There are couple of problems:

  1. split(" the") uses " the" as delimiter and gives rest of the words. Best is to split using whitespace.
  2. Use token.equals("the") instead of ==.
Dakshinamurthy Karra
  • 5,353
  • 1
  • 17
  • 28
0

if you want to count number of occurrence use this sample code:

import java.util.*;
public class theCount {
   public static void main(String[] args) {
       Scanner userInput = new Scanner(System.in);
       System.out.print("Enter a sentence: ");
       String sentence = userInput.nextLine();
       int theCount = sentence.length() - sentence.replace("the", "").length();
       System.out.print("Number of occurrence: " + theCount);
   }
}
  • It worked buy I needed to divide theCount by three in order to get the number of times the word appears in the program. Since your equation counts the number of times 't','h',and 'e' appear in the string. – Jason_Silva Nov 20 '16 at 18:45
0

You can add the input to a arraylist and then can play around with it.

One way is to get the count from frequency method.

List<String> arrayList = new ArrayList<String>();
arrayList.add("String"); //add all the words.

Collections.frequency(arrayList, "the");

Second way is to get the count from map.

Map<String, Integer> map = new HashMap<String, Integer>();
for(String s : arrayList){
        Integer count = map.get(s);
        map.put(s, count==null?1:count+1);
} 
//the below will give you the count of any word.
map.get("the");
user2649233
  • 912
  • 4
  • 14
  • 28
0

As of Java 8 you could you stream api to solve this. That will be more concise. Take the following code as example

public static void main(String[] args) {
    String str = "The is the for THE and the the the the The The";

    long count = Stream.of(str.split(" "))
            .filter(i -> i.equalsIgnoreCase("the"))
            .count();

    System.out.println(count);
}

=== Update ===

public static void main(String[] args) {

    String str = " there these theology";

    long count = Stream.of(str.split(" "))
            .map(String ::toLowerCase)
            .filter(i -> i.contains("the"))
            .count();

    System.out.println(count);
}

=== Update ===

This solution will work even there are multiple same sub strings in a string.

public static void main(String[] args) {

    String str = " thesethefajfskfjthetheasdfjasdkfjthe";
    String findStr = "the";

    int count = 0;
    for (String s : str.split(" ")) {
        count += s.toLowerCase()
                .split(findStr, -1).length - 1 ;
    }

    System.out.println(count);

}

This SO post will help you to understand, how to find all sub string in a single string.

Community
  • 1
  • 1
seal
  • 1,122
  • 5
  • 19
  • 37
  • Will this work for words like there these theology? Since the letters from 'the' are within them. – Jason_Silva Nov 20 '16 at 18:09
  • @Jason_Silva I have update my answer. See the update portion. This should meet your requirement. For more clearance, do you also need to consider those `the` word that is sub string of another word ? Like `kdfasfjTHEkfskf` this word contain one the as a sub string. – seal Nov 20 '16 at 18:43