Results 1 to 9 of 9
- 02-06-2011, 06:29 PM #1
How to break down a sentence String?
I have a class named SentenceStats
And inside SentenceStats I need to 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 (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 capital
All I need is some hints as to where to start. What methods to use. Im fairly new to Java and this one is difficult.
- 02-06-2011, 06:34 PM #2
What I have so far.
public class SentenceStats
{
private String sentence;
public SentenceStats(String sentence){
this.sentence=sentence;
}
public String getSentence(){
return sentence;
}
}
-
One way to break up a String by a space or any character string is to use the split(..) method. You call it by doing something like myString.split(" "); It returns an array of all the subStrings that are separated by the expression passed into the split method. That expression is actually a regular expression which has special properties and often requires extra care, but for your simple example, you don't have to worry about this. If you are not allowed to use split, then you could use the indexOf methods of String to find the location of the spaces and then subString to extract the String.
Once you have your words, you would likely use a for loop to iterate through it using the String's length and the charAt method, the former to tell you how many times to loop the for loop and the latter to extract the char at the loop index position.
- 02-06-2011, 07:03 PM #4
Thanks that's very helpful.
-
- 02-06-2011, 09:34 PM #6
So this what Ive written... This is without the method calcStats. The problem is that the code is giving the total number of Capitals in the string per println but not the amount of capitals per subString.
Java Code:import java.util.Scanner; public class SentenceStats { public static void main(String args[]){ Scanner scanner = new Scanner(System.in); String sentence = new String(); System.out.println("Enter sentence: "); sentence = scanner.nextLine(); String[] array = sentence.split(" "); System.out.println(array.length); int isCapital = 0; for(int i=0;i<array.length;i++){ for(int j=0; j<array[i].length();j++){ if(Character.isUpperCase(array[i].charAt(j))) isCapital++; } System.out.println((array[i])+ " - "+ (array[i].length())+ " characters " +(isCapital)+ " capitals. "); } } }Last edited by Fubarable; 02-06-2011 at 10:07 PM. Reason: Moderator edit: code tags added
-
You will likely need two for loops, one nested inside the other to get the data on the subStrings. The first for loop loops through each sub String in the String array, and the second loops through each character in each sub String.
I added code tags to your last post so the code will be readable. To see how to do this yourself, please read the link in my signature below.
- 02-07-2011, 02:52 AM #8
Thank you so much! It was a lot of help. The code works now!
All I need to do is rewrite it with a static method calcStats...
Here's the code;
Java Code:import java.util.Scanner; public class SentenceStats { public static void main(String args[]){ Scanner scanner = new Scanner(System.in); String sentence = new String(); System.out.println("Enter sentence: "); sentence = scanner.nextLine(); String[] array = sentence.split(" "); System.out.println(array.length); int[] isCapital; // declares an array of integers isCapital = new int[array.length]; for(int i=0;i<array.length;i++){ for(int j=0; j<array[i].length();j++){ if(Character.isUpperCase(array[i].charAt(j))) isCapital[i]++; } System.out.println((array[i])+ " - "+ (array[i].length())+ " characters " +(isCapital[i])+ " capitals. "); } } }
-
And you've done it on your own -- good show. It's heart warming to see threads like these where you see a the poster who has the intelligence and motivation to see the problem through to solution.
Similar Threads
-
counting the vowels in a sentence
By huntrguy102 in forum New To JavaReplies: 3Last Post: 11-08-2010, 06:31 AM -
Finding a word in a sentence?
By blackrabbit in forum New To JavaReplies: 6Last Post: 07-22-2010, 11:07 PM -
and after SEARCH for a any string like words or sentence or urls, for how many times
By lse123 in forum Advanced JavaReplies: 0Last Post: 02-25-2010, 12:51 PM -
enter a string sentence
By amorosa19 in forum New To JavaReplies: 11Last Post: 01-28-2009, 04:05 AM -
String/sentence to unicode convertion
By sandeepvreddy in forum New To JavaReplies: 5Last Post: 11-20-2008, 03:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks