Results 1 to 6 of 6
  1. #1
    nisim777 is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default Tokens, delimiters, and all that jazz

    Hello all,

    Let me begin by being perfectly honest. My question does pertain to a homework assignment. However, I am not looking for a solution. I am only looking for some guidance in the right direction. Basically the assignment is this:

    I must create a program that allows the user to input multiple sentences and the program is to capitalize every word at the beginning of the sentence. I have seen a ton of ways online by using regular expressions and such, but we have to use the tools that have been made available to us thus far in our learning (which are basically arrays, StringBuilder, StringTokenizer, and Split). I am well on my way to a solution, but I cannot get past my current hump. I have basically tokenized the string while returning the delimiters (I am using punctuation as a delimiter), but I cannot figure out how to use a delimiter split as a conditional statement in order to capitalize what follows the delimiter. Below is my code. It is still sketchy as I have been trying out a lot of things. I currently only have a period as a delimiter, but I will be adding other punctuation later.

    Thanks for any help.

    Java Code:
    package chapter8;
    
    import java.util.Scanner;
    import java.util.StringTokenizer;
    
    public class SentenceCapitalizer {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		
    		System.out.print("Enter a sentence: ");
    		Scanner keyboard = new Scanner(System.in);
    		
    		String ourWord = keyboard.nextLine();
    		
    		capitalizer(ourWord);
    		
    		
    
    	}
    	
    	public static void capitalizer(String s)
    	{
    		
    		String word = s;
    		String newFirstLetter;
    		
    		String firstLetter = word.substring(0, 1);
    		String rest = word.substring(1);
    		
    		String initialCapitalized = firstLetter.toUpperCase() + rest.toLowerCase();
    		
    		StringTokenizer tokens = new StringTokenizer(initialCapitalized, ".", true);
    		while (tokens.hasMoreTokens())
    		{
    			System.out.println(tokens.nextToken());
    			
    		}
    		
    
    
    		
    		
    	}
    
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I feel like this could be done easiest with split and a string builder. Why not split the string on some delimeter(in this case .) this will produce an array where each element is a string from previous period to next period. From there you can use a loop which does three things, capitalizes the first letter and adds it to the builder, adds the rest of the string, adds a period.

    Java Code:
    get input
    Declare string array, store split input in array
    Declare stringbuilder
    loop
      Append capitalized first letter to stringbuilder 
      append rest of the element to string builder
      append period to string builder
    end loop

  3. #3
    nisim777 is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default re:

    Thanks for the help. I was able to figure it out with the exception of a single bug.

    Thanks again.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Perhaps we can help you fix the bug. If you would like; explain the bug and any relevant code snippets. If you are satisfied without fixing the bug please mark your thread solved with the thread tools at the top.

  5. #5
    nisim777 is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default re:

    Basically, the code double prints the first letter of any sentence other than the first sentence. Here is the code.

    Java Code:
    package chapter8;
    
    import java.util.Scanner;
    import java.util.StringTokenizer;
    
    public class SentenceCapitalizer {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		
    		System.out.print("Enter a sentence: ");
    		Scanner keyboard = new Scanner(System.in);
    		
    		String ourWord = keyboard.nextLine();
    		
    		capitalizer(ourWord);
    		
    		
    
    	}
    	
    	public static void capitalizer(String s)
    	{
    		
    		String word = s;
    		
    		//String[] sentences;
    		
    		StringTokenizer sentences = new StringTokenizer(word, ".", true);
    		StringBuilder newSentence = new StringBuilder();
    		
    		while (sentences.hasMoreTokens())
    		{
    			String token = sentences.nextToken();
    			String token1;
    			if (token.substring(0, 1).equals(" "))
    			{
    				token1 = token.substring(1, 2).toUpperCase();
    				token1 = " " + token1;
    			}
    			else
    			{
    				token1 = token.substring(0, 1).toUpperCase();
    			}
    				String token2 = token.substring(1);
    			
    			newSentence.append(token1);
    			newSentence.append(token2);
    			
    			
    			
    		}
    		System.out.print(newSentence.toString());
    		
    	}
    
    }

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The best way to figure this out is to input a sentence that tests both if clauses. Have a sentence like this
    Java Code:
    how. are.you. doing. today?
    And see which words contain doubles. This will help you pinpoint WHICH if clause is incorrect.

    hint: it has something to do with incorrect substring calls.

Similar Threads

  1. Replies: 10
    Last Post: 04-12-2011, 06:00 PM
  2. String Tokenizer, no delimiters
    By fuzzdn in forum New To Java
    Replies: 3
    Last Post: 12-30-2010, 02:56 PM
  3. parsing multiple delimiters
    By meshhat in forum New To Java
    Replies: 3
    Last Post: 04-19-2009, 12:51 AM
  4. Read file delimiters
    By GraemeH in forum New To Java
    Replies: 4
    Last Post: 03-29-2009, 11:44 AM
  5. Replies: 4
    Last Post: 07-07-2008, 01:32 PM

Posting Permissions

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