This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes hey can you help me please? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "hey can you help me please?" Watch "hey can you help me please?" New topic
Author

hey can you help me please?

Paul Chung
Greenhorn

Joined: Feb 10, 2005
Posts: 4
hey guys, I'm trying to make a program that modifies an input string (via JOptionPane.showInputDialog) so that the first character of a word is always capitalized and every other character is in lowercase. For instance if I inputted "hey yall" the program should output "Hey Yall". So far I've made it so that this works so long as the first character is a letter. However the code I've written doesn't work if the first character is a non letter character (such as _ ). So if I input "_hey yall" the output looks like "_hey Yall". This is a school project, so I understand if you don't want to give me the answer directly, but your help in this matter would be greatly appreciated!

The code is:

import javax.swing.JOptionPane;

public class string
{
public static void main(String[] args)
{
String input = JOptionPane.showInputDialog(null, "Sentence", "Sentence");

int index;
int spaceFlag = 0;
char charIndex;


for (index = 0; index <= input.length() - 1; index++)
{
charIndex = input.charAt(index);

if (index == 0)
{
if( (charIndex >= 97) && (charIndex <= 122) )
{
charIndex -= 32;
}
}
// THIS PART IS GIVING ME TROUBLE
else
if ( ( Character.isWhitespace( charIndex ) ) )
{


while ( Character.isWhitespace( charIndex))
{
System.out.print( charIndex );

spaceFlag = index + 1;
charIndex = input.charAt(spaceFlag);
if ( ( charIndex >= 97) && ( charIndex <= 122) )
{
charIndex -= 32;
spaceFlag = 0;

}
index++;


}

}
*/
else
if (index != 0)
{
if (( charIndex >= 65) && ( charIndex <= 90))
{
charIndex += 32;
}
else
if ( ( charIndex == 32) && ( charIndex < 65) )
{
// Trouble spot

while ( charIndex == 32)
{
System.out.print( charIndex );

spaceFlag = index + 1;
charIndex = input.charAt(spaceFlag);
if ( ( charIndex >= 97) && ( charIndex <= 122) )
{
charIndex -= 32;
spaceFlag = 0;

}
index++;


}

}

}

System.out.print( charIndex );

}

System.exit(0);
}
}

Thanks again. If I misused some terms, please feel free to correct me (be gentle) heh.

- Paul
Yosi Hendarsjah
Ranch Hand

Joined: Oct 02, 2003
Posts: 164
How can you capitalize an underscore ("_")? Maybe you want to capitalize the letter after the underscore?
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I don't have any suggestions off the top of my head to answer your question directly. However, I have a suggestion to help make your code more readable: use character constants rather than ASCII values. By this I mean that instead of using the number 32, you should use the character literal ' '; instead of 97, use 'a'. (Character constants are simply a single character surrounded by single quotes.) Not only will it help others here to understand your code, but if you come back to this at a later time, YOU will be able to more easily understand what you did.

Also, when you post code, you should use UBB CODE tags. There are shortcut buttons below the text area where you type a message for this forum.

I'm sorry that I can't answer your question more directly, but perhaps if you make this change it may help you find the problem on your own.

Keep Coding!

Layne


Java API Documentation
The Java Tutorial
Kenneth Brooks
Greenhorn

Joined: Feb 21, 2005
Posts: 9
you are in the general area of the answer.

you already found the isWhitespace, so perhaps you could
replace some of your logic like:



using isLetter (which will help the readability of your code)
and toUppercase

ponder what you can do with those and get back to us.

Good Luck!
joseph mcgratton
Ranch Hand

Joined: Feb 15, 2005
Posts: 41
i would just lay down rules, so that if the first character is "0" "1" "2" etc, you print an error message to the user stating "Yout first character cannot be a number".
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
I would probably use compareTo i.e.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: hey can you help me please?