Results 1 to 9 of 9
  1. #1
    Georgiafbi's Avatar
    Georgiafbi is offline Member
    Join Date
    Feb 2011
    Location
    Georgia
    Posts
    5
    Rep Power
    0

    Question 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.

  2. #2
    Georgiafbi's Avatar
    Georgiafbi is offline Member
    Join Date
    Feb 2011
    Location
    Georgia
    Posts
    5
    Rep Power
    0

    Default What I have so far.

    public class SentenceStats
    {
    private String sentence;


    public SentenceStats(String sentence){
    this.sentence=sentence;
    }

    public String getSentence(){
    return sentence;
    }

    }

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  4. #4
    Georgiafbi's Avatar
    Georgiafbi is offline Member
    Join Date
    Feb 2011
    Location
    Georgia
    Posts
    5
    Rep Power
    0

    Default

    Thanks that's very helpful.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Georgiafbi View Post
    Thanks that's very helpful.
    You're welcome. There's more that you have to do then I mentioned, but if you break the problem down and solve each issue one at a time, you'll solve this. Much luck and welcome to the Java-Forums!

  6. #6
    Georgiafbi's Avatar
    Georgiafbi is offline Member
    Join Date
    Feb 2011
    Location
    Georgia
    Posts
    5
    Rep Power
    0

    Default

    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

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  8. #8
    Georgiafbi's Avatar
    Georgiafbi is offline Member
    Join Date
    Feb 2011
    Location
    Georgia
    Posts
    5
    Rep Power
    0

    Default

    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. ");
        }
            
            }
            
    
        }

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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

  1. enter a string sentence
    By amorosa19 in forum New To Java
    Replies: 13
    Last Post: 09-06-2013, 10:34 AM
  2. counting the vowels in a sentence
    By huntrguy102 in forum New To Java
    Replies: 3
    Last Post: 11-08-2010, 06:31 AM
  3. Finding a word in a sentence?
    By blackrabbit in forum New To Java
    Replies: 6
    Last Post: 07-22-2010, 11:07 PM
  4. Replies: 0
    Last Post: 02-25-2010, 12:51 PM
  5. String/sentence to unicode convertion
    By sandeepvreddy in forum New To Java
    Replies: 5
    Last Post: 11-20-2008, 03:33 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •