Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a Java Expert!

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!




Text Processing

 
Reply to this topicStart new topic

Text Processing

raeNet
9 Dec, 2008 - 10:55 AM
Post #1

D.I.C Head
**

Joined: 1 Nov, 2007
Posts: 136



Thanked: 1 times
My Contributions
QUOTE
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(" . ");

        for (String input : strarray)
        {

        String capitalize = input.substring(0, 1).toUpperCase() +
                        input.substring(0).toLowerCase() + " ";

        //Display input with first word in each sentence capitalized
        JOptionPane.showMessageDialog(null, capitalize);
        }

        System.exit(0);
    }
}


User is offlineProfile CardPM
+Quote Post


Locke
RE: Text Processing
9 Dec, 2008 - 11:19 AM
Post #2

I am *not* a Thief...I *prefer* TREASURE HUNTER!
Group Icon

Joined: 20 Mar, 2008
Posts: 2,808



Thanked: 185 times
Dream Kudos: 325
Expert In: Java

My Contributions
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.

java
String capitalize = input.substring(0, 1).toUpperCase() + 
input.substring(1, input.length() - 1);


Try that.

And for future reference, please give us an example of your program's execution. Your description of your problem was a little unclear.

Hope this helps! biggrin.gif

This post has been edited by Locke37: 9 Dec, 2008 - 11:22 AM
User is offlineProfile CardPM
+Quote Post

raeNet
RE: Text Processing
9 Dec, 2008 - 11:39 AM
Post #3

D.I.C Head
**

Joined: 1 Nov, 2007
Posts: 136



Thanked: 1 times
My Contributions
Thank you. It did work for the 1st sentence. However, if I enter 2 sentences, the 2nd is still lowercase. Any suggestions?

My input: hi. my name is Joe.
Current output: Hi. my name is Joe.
Should be: Hi. My name is Joe.
User is offlineProfile CardPM
+Quote Post

pbl
RE: Text Processing
9 Dec, 2008 - 09:22 PM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 6,951



Thanked: 673 times
Dream Kudos: 200
My Contributions
I don't why you use

String[] strarray = s.split(" . ");

You are splitting at " . "
May be you should use a StringTokenizer and apply .toUppercase() to the first token returned
User is offlineProfile CardPM
+Quote Post

raeNet
RE: Text Processing
10 Dec, 2008 - 01:32 AM
Post #5

D.I.C Head
**

Joined: 1 Nov, 2007
Posts: 136



Thanked: 1 times
My Contributions
QUOTE
Well, I tried the string tokenizer, but without any luck. Results:

Input: hi. my name is Joe.
Output1: H [Ok]
Output2: my name is jo [Ok]
Should be: Hi. My name is Joe.


What am I doing wrong???? I've been at this all night. Any help is greatly appreciated.

CODE

import javax.swing.JOptionPane;
import java.util.StringTokenizer;

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("[\\?!.]");
    
    for (String s : tokens)
    
        {

        String capitalize = s.substring(0, 1).toUpperCase() +
                        s.substring(1, s.length() - 1);
      
        //Show input capitalized
        JOptionPane.showMessageDialog(null, capitalize);
        }

        System.exit(0);
    }
}

User is offlineProfile CardPM
+Quote Post

raeNet
RE: Text Processing
10 Dec, 2008 - 08:18 AM
Post #6

D.I.C Head
**

Joined: 1 Nov, 2007
Posts: 136



Thanked: 1 times
My Contributions
Can anyone tell me if I'm getting any closer to solving this? Thanks in advance.
User is offlineProfile CardPM
+Quote Post

Locke
RE: Text Processing
10 Dec, 2008 - 09:02 AM
Post #7

I am *not* a Thief...I *prefer* TREASURE HUNTER!
Group Icon

Joined: 20 Mar, 2008
Posts: 2,808



Thanked: 185 times
Dream Kudos: 325
Expert In: Java

My Contributions
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 (x == 0)
capitalize += tokens[x].charAt(0).toUpperCase() + tokens[x].substring(1, tokens[x].length() - 1) + " ";

else if (tokens[x - 1].endsWith("."))
capitalize += " " + tokens[x].charAt(0).toUpperCase() + tokens[x].substring(1, tokens[x].length() - 1) + " ";
}


That might work.

Hope this helps! biggrin.gif

EDIT: This won't do question marks or exclamation points, as I just threw this together.

This post has been edited by Locke37: 10 Dec, 2008 - 09:22 AM
User is offlineProfile CardPM
+Quote Post

raeNet
RE: Text Processing
10 Dec, 2008 - 09:51 AM
Post #8

D.I.C Head
**

Joined: 1 Nov, 2007
Posts: 136



Thanked: 1 times
My Contributions
QUOTE(Locke37 @ 10 Dec, 2008 - 09:02 AM) *

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 (x == 0)
capitalize += tokens[x].charAt(0).toUpperCase() + tokens[x].substring(1, tokens[x].length() - 1) + " ";

else if (tokens[x - 1].endsWith("."))
capitalize += " " + tokens[x].charAt(0).toUpperCase() + tokens[x].substring(1, tokens[x].length() - 1) + " ";
}


That might work.

Hope this helps! biggrin.gif

EDIT: This won't do question marks or exclamation points, as I just threw this together.




QUOTE
Wow - it looked/sounded great, but when added I received the following errors:

sentenceTok.java:11: cannot find symbol
symbol : variable s
location: class sentenceTokDic
String[] tokens = s.split(" ");
^
sentenceTokDic.java:18: char cannot be dereferenced
capitalize += tokens[x].charAt(0).toUpperCase() + tokens[x].substring(1, tokens[x].length() - 1) + " ";
^
sentenceTokDic.java:21: char cannot be dereferenced
capitalize += " " + tokens[x].charAt(0).toUpperCase() + tokens[x].substring(1, tokens[x].length() - 1) + " ";
^
sentenceTokDic.java:26: cannot find symbol
symbol : variable capitalize
location: class sentenceTokDic
JOptionPane.showMessageDialog(null, capitalize);
^
4 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

User is offlineProfile CardPM
+Quote Post

Locke
RE: Text Processing
10 Dec, 2008 - 09:59 AM
Post #9

I am *not* a Thief...I *prefer* TREASURE HUNTER!
Group Icon

Joined: 20 Mar, 2008
Posts: 2,808



Thanked: 185 times
Dream Kudos: 325
Expert In: Java

My Contributions
The first one is easy, just change s.split(" "); to str.split(" ");...

I don't know why you're getting the next 2.

The last one...just move the String capitalize = null; line outside of the for loop.
User is offlineProfile CardPM
+Quote Post

raeNet
RE: Text Processing
10 Dec, 2008 - 10:14 AM
Post #10

D.I.C Head
**

Joined: 1 Nov, 2007
Posts: 136



Thanked: 1 times
My Contributions
QUOTE
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)");

    String[] tokens = str.split(" ");
    String capitalize = null;

    for (int x = 0; x < tokens.length; x++) // edit the loop so we can access values related to the current one
    {
    
   if (x == 0)
      capitalize += tokens[x].charAt(0).toUpperCase() + tokens[x].substring(1, tokens[x].length() - 1) + " ";

   else if (tokens[x - 1].endsWith("."))
      capitalize += " " + tokens[x].charAt(0).toUpperCase() + tokens[x].substring(1, tokens[x].length() - 1) + " ";
}

       //Show input capitalized
        JOptionPane.showMessageDialog(null, capitalize);
        }
        }

User is offlineProfile CardPM
+Quote Post

Locke
RE: Text Processing
10 Dec, 2008 - 12:23 PM
Post #11

I am *not* a Thief...I *prefer* TREASURE HUNTER!
Group Icon

Joined: 20 Mar, 2008
Posts: 2,808



Thanked: 185 times
Dream Kudos: 325
Expert In: Java

My Contributions
Ok, this works just how you want it to. With sentences only. No questions or anything. Just periods. smile.gif

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

String[] tokens = str.split(" ");
String capitalize = "";

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);

else if (tokens[x - 1].endsWith(".") || tokens[x - 1].equals(""))
capitalize += " " + tokens[x].substring(0, 1).toUpperCase() + tokens[x].substring(1, tokens[x].length() - 1);

// ADDED this, because what if none of the above are true?
else
capitalize += " " + tokens[x];
}
}

capitalize += "."; // end with a period.

//Show input capitalized
JOptionPane.showMessageDialog(null, capitalize);
}
}


I'll leave determining whether or not to add the question and exclamation functionality.

Hope this helps! biggrin.gif

This post has been edited by Locke37: 10 Dec, 2008 - 01:52 PM
User is offlineProfile CardPM
+Quote Post

raeNet
RE: Text Processing
10 Dec, 2008 - 01:37 PM
Post #12

D.I.C Head
**

Joined: 1 Nov, 2007
Posts: 136



Thanked: 1 times
My Contributions
QUOTE(Locke37 @ 10 Dec, 2008 - 12:23 PM) *

Ok, this works just how you want it to. With sentences only. No questions or anything. Just periods. smile.gif

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

String[] tokens = str.split(" ");
String capitalize = "";

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);

else if (tokens[x - 1].endsWith(".") || tokens[x - 1].equals(""))
capitalize += " " + tokens[x].substring(0, 1).toUpperCase() + tokens[x].substring(1, tokens[x].length() - 1);

// ADDED this, because what if none of the above are true?
else
capitalize += " " + tokens[x];
}
}

capitalize += "."; // end with a period.

//Show input capitalized
JOptionPane.showMessageDialog(null, capitalize);
}
}

Hope this helps! biggrin.gif




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 sentencessmile.gif

display is: Hi M name is Joe..
(with onespace between sentencessmile.gif

display is: Hi.m name is Joe..
(with zero spaces between sentencessmile.gif

I'll try to play with it some more. If you have any more suggestions, I'm all ears...

Again, you've been most helpful!



User is offlineProfile CardPM
+Quote Post

Locke
RE: Text Processing
10 Dec, 2008 - 02:06 PM
Post #13

I am *not* a Thief...I *prefer* TREASURE HUNTER!
Group Icon

Joined: 20 Mar, 2008
Posts: 2,808



Thanked: 185 times
Dream Kudos: 325
Expert In: Java

My Contributions
...So, it's still not right? unsure.gif

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. wink2.gif

This post has been edited by Locke37: 10 Dec, 2008 - 02:11 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 02:22PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month