Results 1 to 6 of 6
- 04-17-2011, 06:24 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
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()); } } }
- 04-17-2011, 06:29 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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
- 04-18-2011, 12:33 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
re:
Thanks for the help. I was able to figure it out with the exception of a single bug.
Thanks again.
- 04-18-2011, 12:36 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 04-18-2011, 12:41 AM #5
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
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()); } }
- 04-18-2011, 01:07 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
The best way to figure this out is to input a sentence that tests both if clauses. Have a sentence like this
And see which words contain doubles. This will help you pinpoint WHICH if clause is incorrect.Java Code:how. are.you. doing. today?
hint: it has something to do with incorrect substring calls.
Similar Threads
-
how to parse an input files that have different types of delimiters
By renu in forum New To JavaReplies: 10Last Post: 04-12-2011, 06:00 PM -
String Tokenizer, no delimiters
By fuzzdn in forum New To JavaReplies: 3Last Post: 12-30-2010, 02:56 PM -
parsing multiple delimiters
By meshhat in forum New To JavaReplies: 3Last Post: 04-19-2009, 12:51 AM -
Read file delimiters
By GraemeH in forum New To JavaReplies: 4Last Post: 03-29-2009, 11:44 AM -
Extracting words from a string using delimiters
By toad in forum New To JavaReplies: 4Last Post: 07-07-2008, 01:32 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks