• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

hey can you help me please?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 168
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can you capitalize an underscore ("_")? Maybe you want to capitalize the letter after the underscore?
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would probably use compareTo i.e.

 
eat bricks! HA! And here's another one! And a tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic