Java word count program

后端 未结 22 980
北荒
北荒 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:26

    You need to read the file line by line and reduce the multiple occurences of the whitespaces appearing in your line to a single occurence and then count for the words. Following is a sample:

    public static void main(String... args) throws IOException {   
    
        FileInputStream fstream = new FileInputStream("c:\\test.txt");
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        int wordcount = 0;
        while ((strLine = br.readLine()) != null)   {
            strLine = strLine.replaceAll("[\t\b]", "");
            strLine = strLine.replaceAll(" {2,}", " ");
            if (!strLine.isEmpty()){
                wordcount = wordcount + strLine.split(" ").length;
            }
        }
    
        System.out.println(wordcount);
        in.close();
    }
    
    0 讨论(0)
  • 2020-12-09 07:26
     public class CountWords 
        {
            public static void main (String[] args)
            {
                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 = " + wordCount));
            }
        }   
    

    This gives the correct result because if space comes twice or more then it can't increase wordcount. Enjoy.

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

    you should make your code more generic by considering other word separators as well.. such as "," ";" etc.

    public class WordCounter{
        public int count(String input){
            int count =0;
            boolean incrementCounter = false;
            for (int i=0; i<input.length(); i++){
                if (isValidWordCharacter(input.charAt(i))){
                    incrementCounter = true;
                }else if (incrementCounter){
                    count++;
                    incrementCounter = false;
                }
            }
            if (incrementCounter) count ++;//if string ends with a valid word
            return count;
        }
        private boolean isValidWordCharacter(char c){
            //any logic that will help you identify a valid character in a word
            // you could also have a method which identifies word separators instead of this
            return (c >= 'A' && c<='Z') || (c >= 'a' && c<='z'); 
        }
    }
    
    0 讨论(0)
  • 2020-12-09 07:27
    public static int CountWords(String str){
    
       if(str.length() == 0)
              return 0;
    
       int count =0;
       for(int i=0;i< str.length();i++){
    
    
          if(str(i) == ' ')
              continue;
    
          if(i > 0 && str.charAt(i-1) == ' '){
            count++;
          } 
    
          else if(i==0 && str.charAt(i) != ' '){
           count++;
          }
    
    
       }
       return count;
    
    }
    
    0 讨论(0)
提交回复
热议问题