Java Beginner - Counting number of words in sentence

后端 未结 17 1671
忘了有多久
忘了有多久 2021-01-01 05:29

I am suppose to use methods in order to count number of words in the a sentence. I wrote this code and I am not quite sure why it doesn\'t work. No matter what I write, I on

相关标签:
17条回答
  • 2021-01-01 06:01

    Please check for boundary conditions at str.charAt(i+1). It can result in a StringIndexOutOfBoundsException when the string is terminated by a space.

    1. Account for cases where the string starts with a ' '
    2. The String could be blank.
    3. The definition of a 'word' could differ. For example - '1 2'. Are 1,2 words ?

    I know you do not want an alternate method but string.split(" ").length() is an easier way to start.

    0 讨论(0)
  • 2021-01-01 06:02

    i<=str.length()-1 should probably be i<str.length()-1 or you will get an IndexOutOfBoundsException at str.charAt(i+1) (the available values are str.charAt(0) to str.charAt(str.length()-1)).

    0 讨论(0)
  • 2021-01-01 06:05

    import java.util.HashMap;

    import java.util.Map;

    import java.util.Scanner;

    public class WordrCount {

    public static void main(final String[] args) {
        System.out.println("Please Enter Your String: ");
        final Map<String, Integer> hm = new HashMap<String, Integer>();
        final Scanner sc = new Scanner(System.in);
        final String s1 = sc.nextLine();
        final String[] c1 = s1.split(" ");
        for (int i = 0; i < c1.length; i++) {
            if (!hm.containsKey(c1[i])) {
                hm.put(c1[i], (Integer)1);
            }// if
            else {
                hm.put(c1[i], hm.get(c1[i]) +(Integer) 1);
    
            }// else
        }// for
    
        System.out.println("The Total No Of Words: " + hm);
    
    }// main
    

    }// WordCount

    0 讨论(0)
  • 2021-01-01 06:05
    class Words {
    public static void main(String[] args) {
    
        String str="Hello World this is new";
        str=str.trim();
        int n=str.length();
        char a[]=new char[n];
        str.getChars(1,n,a,0);
        for (int i=0;i<str.length() ; i++){
            if(a[i]==' ') count++;
        }
        System.out.println("No. of words = "+(count+1));
    }
    

    }

    0 讨论(0)
  • 2021-01-01 06:06

    This is how i have done it :

    It works fine too

        import java.util.Scanner;
    
    
        public class countwords {
    
            public static void main(String args[]){
                Scanner in=new Scanner(System.in);
                System.out.println("Enter your sentence:[Try to ignore space at end]");
                String s=in.nextLine();
                System.out.println("Size of the string is "+s.length());
                int res=count(s);
                System.out.println("No of words in the given String --->>"+"  "+s+" "+"is"+" :"+res);
            }
    
    
            private static int count(String s) {
                // TODO Auto-generated method stub
                int count=0;
                if(s.charAt(0)!=' '){
                    count++;
                }
                for(int i=0;i<s.length();i++){
                    if((s.charAt(i)==' ')){
                        count++;
                    }
                }
                return count;
            }
    
    
    }
    

    OUTPUT: this is my world bonaza

    Size of the string is 23

    No of words in the given String --->> this is my world bonazais :5

    There are some bugs try to correct them and repost

    A more simple way that i found was :

    Just use this :

        // Read a string
    String st=s.nextLine();
    
    // Split string with space
    String words[]=st.trim().split(" ");
    
    0 讨论(0)
  • An easy way to solve this problem:

    return str.split(" ").length;
    

    Or to be more careful, this is how you'd take into account more than one whitespace:

    return str.split("\\s+").length;
    
    0 讨论(0)
提交回复
热议问题