Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hey can you help me please?

 
Paul Chung
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can you capitalize an underscore ("_")? Maybe you want to capitalize the letter after the underscore?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kenneth Brooks
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would probably use compareTo i.e.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic