Join 244,283 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,136 people online right now. Registration is fast and FREE... Join Now!
My objective is to capitalize the first word of each sentence the user enters. But, I'm definitely missing something because only the first sentence is modified and it's adding capitalization without replacement. Any suggestions to get this code working? Many thanks in advance!
CODE
import javax.swing.*;
public class sentCaps { public static void main(String[] args) { //Get user sentence(s) String s = JOptionPane.showInputDialog(null, "Please input your sentence(s)"); String[] strarray = s.split(" . ");
Right now, you're not actually changing the sentence any, you're just grabbing characters and displaying them in uppercase. You're not stripping the lower case characters from the original sentence.
So if I input abc def. right now, it'll output Aabc def. (if I understand your problem correctly.)
You need to reuse the substring method again to display the rest of the word and NOT the first character.
public class sentenceTok { public static void main(String[] args) { //Get user input String str = JOptionPane.showInputDialog(null, "Please input your sentence(s)"); String[] tokens = str.split("[\\?!.]");
If you split it by the period, then it will completely remove the period from the sentence(s) entirely. You won't know where it was...where it's supposed to go, etc.
I suggest looking at this from a different way than splitting by the period. Sorry I can't give you much more help than that, since I'm in class right now.
EDIT: Actually...Split your sentences by the space character, then in your for loop, check if the previous portion of the array ends with a period. If it does end with a period...capitalize the current portion.
java
String[] tokens = s.split(" ");
for (int x = 0; x < tokens.length; x++) // edit the loop so we can access values related to the current one { String capitalize = null;
If you split it by the period, then it will completely remove the period from the sentence(s) entirely. You won't know where it was...where it's supposed to go, etc.
I suggest looking at this from a different way than splitting by the period. Sorry I can't give you much more help than that, since I'm in class right now.
EDIT: Actually...Split your sentences by the space character, then in your for loop, check if the previous portion of the array ends with a period. If it does end with a period...capitalize the current portion.
java
String[] tokens = s.split(" ");
for (int x = 0; x < tokens.length; x++) // edit the loop so we can access values related to the current one { String capitalize = null;
Here's the code w/revisions. I'm still getting the last two errors. Am I missing something?
CODE
public class sentenceTok { public static void main(String[] args) { //Get user input String str = JOptionPane.showInputDialog(null, "Please input your sentence(s)");
Ok, this works just how you want it to. With sentences only. No questions or anything. Just periods.
The 2 errors you got were from the charAt(0) method I used. Turns out you can't dereference a char type...my mistake. I got ahead of myself.
Anyway...to the code!
java
import javax.swing.*;
public class sentenceTok { public static void main(String[] args) { //Get user input String str = JOptionPane.showInputDialog(null, "Please input your sentence(s)");
for (int x = 0; x < tokens.length; x++) // edit the loop so we can access values related to the current one { if (!tokens[x].equals("")) // if we didn't have this...things would go wrong. { if (x == 0) capitalize += tokens[x].substring(0, 1).toUpperCase() + tokens[x].substring(1, tokens[x].length() - 1);
Ok, this works just how you want it to. With sentences only. No questions or anything. Just periods.
The 2 errors you got were from the charAt(0) method I used. Turns out you can't dereference a [color=#0000FF]char[.color] type...my mistake. I got ahead of myself.
Anyway...to the code!
java
import javax.swing.*;
public class sentenceTok { public static void main(String[] args) { //Get user input String str = JOptionPane.showInputDialog(null, "Please input your sentence(s)");
for (int x = 0; x < tokens.length; x++) // edit the loop so we can access values related to the current one { if (!tokens[x].equals("")) // if we didn't have this...things would go wrong. { if (x == 0) capitalize += tokens[x].substring(0, 1).toUpperCase() + tokens[x].substring(1, tokens[x].length() - 1);
I'll leave determining whether or not to add the question and exclamation functionality.
QUOTE
Hi Locke37. First, many thanks for all your efforts helping me get closer to the solution.
When I run this code after latest revisions, the "period" from the first sentence string is omitted, but included after the punctuation from the second sentence string. When tested with input: hi. my name is Joe. [ok]
display is: Hi my name is Joe.. (with two spaces between sentences
display is: Hi M name is Joe.. (with onespace between sentences
display is: Hi.m name is Joe.. (with zero spaces between sentences
I'll try to play with it some more. If you have any more suggestions, I'm all ears...
I coulda sworn it did it right. Maybe I was little too hasty...I don't know why the first period is being omitted. That doesn't really make sense to me right now.
OH GOD. I know why it puts 2 periods at the end...
Change the following.
java
} // END FOR LOOP
if (!capitalize.endsWith(".")) capitalize += "."; // same line, just add the 'if'
That will make it use 1 period, no matter what. See, the reason it ended with 2 periods was because if you actually put one on your input, it would automatically attach another. But now since I added that if statement, it won't add one if there is already one there.
I'll have a look deeper into this when I have a chance.
This post has been edited by Locke37: 10 Dec, 2008 - 02:11 PM