Searched online and found ways that people are able to do this but I can't seem to get mine to incorporate all the words. Here is what I'm trying to do followed by my 7th try at coding it. Thanks in advance for any advise you may be albe to offer.

2) Inside this project create a class named SentenceStats
3) Inside SentenceStats create a static method named calcStats that accepts a sentence as a String
and prints to the screen the following statistics:

a. The number of words (a words are separated by a single blank space)
b. For each word in the String, print:

i. The word
ii. A hyphen (‐)
iii. The number of characters
iv. The number of uppercase letters

For example, if you passed in the String:
"A man, a plan, a canal, Panama"
The output would be:
7 words
A – 1 characters, 1 capitals
man, – 4 characters, 0 capitals
a – 1 characters, 0 capitals
plan, – 5 characters, 0 capitals
a – 1 characters, 0 capitals
canal, ‐ 6 characters, 0 capitals
Panama – 6 characters, 1 capitals

public class SentenceStats
{
    // instance variables - replace the example below with your own
    private String sentence;
    private int wordCount = 0;
    private int capitalCount;
    private int characterCount;
    private int length;
    /**
     * Constructor for objects of class SentenceStats
     */
    public  SentenceStats(String sentence)
    {
        // initialise instance variables
        //String sentence = sentence;

        wordCount = 1;
        capitalCount = 0;
        characterCount = 0;
        length = sentence.length();
    }


        public  String calcStats(String sentence) 
    {

        for (int count = 0; count < sentence.length(); count++)
        {
            char current = sentence.charAt(count);

            if (Character.isUpperCase(current))
            {
                capitalCount++;
            }

            if (Character.isSpaceChar(current))
            {
                wordCount++;
            }
            //             {
            //                 return capitalCount + " ";
            //             }

        }
        {
            return capitalCount + " " + wordCount + " ";
        }

        //             System.out.println(wordCount + "Words");
        //             System.out.println(sentence.toString());
    }

    // {
    //             return "yes";
    //         }

}

//     public int getCapitalCount()
//     {
//         return capitalCount;
//     }

Recommended Answers

All 2 Replies

Member Avatar for ztini

Looks good so far; try this on for size though:

public class SentenceStats {
	
	public static void calcStats(String s) {
		String[] sentence = s.split(" ");
		
		System.out.println(sentence.length + " words");
		
		for (String word : sentence) {
			int capitalCount = 0;

			for (char letter : word.toCharArray()) {
				if (Character.isUpperCase(letter))
					capitalCount++;
			}

			System.out.println(word + " - " + word.length() + 
					" characters, " + capitalCount + " capitals");		
		}
	}
	
	public static void main(String[] args) {
		SentenceStats.calcStats("A man, a plan, a canal, Panama");
	}
}
Member Avatar for ztini

Duplicate post.

><

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.