Java word count program

后端 未结 22 982
北荒
北荒 2020-12-09 06:48

I am trying to make a program on word count which I have partially made and it is giving the correct result but the moment I enter space or more than one space in the string

相关标签:
22条回答
  • 2020-12-09 07:06

    Two routes for this. One way would be to use regular expressions. You can find out more about regular expressions here. A good regular expression for this would be something like "\w+" Then count the number of matches.

    If you don't want to go that route, you could have a boolean flag that remembers if the last character you've seen is a space. If it is, don't count it. So the center of the loop looks like this:

    boolean prevCharWasSpace=true;
    for (int i = 0; i < str1.length(); i++) 
    {
        if (str1.charAt(i) == ' ') {
            prevCharWasSpace=true;
        }
    else{
            if(prevCharWasSpace) wordChar++;
            prevCharWasSpace = false;
    
        }
    }
    

    Update
    Using the split technique is exactly equivalent to what's happening here, but it doesn't really explain why it works. If we go back to our CS theory, we want to construct a Finite State Automa (FSA) that counts words. That FSA may appear as:
    enter image description here
    If you look at the code, it implements this FSA exactly. The prevCharWasSpace keeps track of which state we're in, and the str1.charAt('i') is decideds which edge (or arrow) is being followed. If you use the split method, a regular expression equivalent of this FSA is constructed internally, and is used to split the string into an array.

    0 讨论(0)
  • 2020-12-09 07:06
        String data = "This world is mine";
        System.out.print(data.split("\\s+").length);
    
    0 讨论(0)
  • 2020-12-09 07:12
    public class wordCOunt
    {
    public static void main(String ar[])
    {
    System.out.println("Simple Java Word Count Program");
    
        String str1 = "Today is Holdiay Day";
    
        int wordCount = 1;
    
        for (int i = 0; i < str1.length(); i++) 
        {
            if (str1.charAt(i) == ' '&& str1.charAt(i+1)!=' ') 
            {
                wordCount++;
            } 
        }
    
        System.out.println("Word count is = " +(str1.length()- wordCount));
    }
    

    }

    0 讨论(0)
  • 2020-12-09 07:13

    The full program working is:

    public class main {
    
        public static void main(String[] args) {
    
            logicCounter counter1 = new logicCounter();
            counter1.counter("I am trying to make a program on word count which I have partially made and it is giving the correct result but the moment I enter space or more than one space in the string, the result of word count show wrong results because I am counting words on the basis of spaces used. I need help if there is a solution in a way that no matter how many spaces are I still get the correct result. I am mentioning the code below.");
        }
    }
    
    public class logicCounter {
    
        public void counter (String str) {
    
            String str1 = str;
            boolean space= true;
            int i;
    
            for ( i = 0; i < str1.length(); i++) {
    
                if (str1.charAt(i) == ' ') {
                    space=true;
                } else {
                    i++;
                }
            }
    
            System.out.println("there are " + i + " letters");
        }
    }
    
    0 讨论(0)
  • 2020-12-09 07:14

    Java does have StringTokenizer API and can be used for this purpose as below.

    String test = "This is a test app";
    int countOfTokens = new StringTokenizer(test).countTokens();
    System.out.println(countOfTokens);
    

    OR

    in a single line as below

    System.out.println(new StringTokenizer("This is a test app").countTokens());
    

    StringTokenizer supports multiple spaces in the input string, counting only the words trimming unnecessary spaces.

    System.out.println(new StringTokenizer("This    is    a test    app").countTokens());
    

    Above line also prints 5

    0 讨论(0)
  • 2020-12-09 07:14

    You can use this code.It may help you:

    public static void main (String[] args)
    {
    
       System.out.println("Simple Java Word Count Program");
    
       String str1 = "Today is Holdiay Day";
       int count=0;
       String[] wCount=str1.split(" ");
    
       for(int i=0;i<wCount.length;i++){
            if(!wCount[i].isEmpty())
            {
                count++;
            }
       }
       System.out.println(count);
    }
    
    0 讨论(0)
提交回复
热议问题