Re: Converting Cases Help
Quote:
Originally Posted by
Bradshjo
I can't get this to work:
Have the name (first and last) appear using the following format: all letters are lower case except for the first letter of the first and last name: (e.g., John Betterdriver). The input for driver one should be exactly as shown above, for example enter in the name: john ‘betterDRIVER’. The output should have the name appear as ‘John Betterdriver’.
Any tips on what I need to do?
Sounds like an introductory programming question, so I'll lead you to it rather than just solve it for you.
First create a string and assign the value. String name="john betterDRIVER". Assuming that you're using an IDE like BlueJay, NetBeans or Eclipse,type -> name.
This will bring up a list of all the methods available to you. You'll notice that there is a toLowerCase() method.
That will be very handy to you.
Next, you'll 2 more strings. One for the first letter, and one for everything that follows.
String firstLetter, theRest;
Use the substring to chop off the first character and put it into the firstLetter. Then use substring to chop off the rest of the string and put it into theRest.
Use toUpperCase() to change the firstLetter. Now add the two strings together.
For instance you could output them with System.out.println(firstLetter + theRest);
This is how you would capitalize each word, but you'll have to do it for all of them. So, you'll need to have a loop. For that, look at .indexOf(). You'll see that you can "find" the first occurrence of a string within a string. So, look for the string containing a space in your original name string. This will let you find the spaces between each word.
There are other really slick ways of doing it, but you wouldn't learn anything from them, and they would be hard to understand.
Post your results here so others can learn from what you've done!
Re: Converting Cases Help
How do you use substring to chop of the rest? I got this so far:
System.out.println("Please enter the Driver's first name: ");
String fName = scan.next();
fName = Character.toUpperCase(fName.charAt(0)) + fName.substring(1);
fRest = ???
Re: Converting Cases Help
First put the whole string toLower() then just toUpper() the first letter, and the letter after a space.
Code java:
String name = "brett tooMEY";
name = name.toLowerCase();
name = name.toUpperCase(name.charAt(0)) + name.substring(1, name.indexOf(" ")) + name.toUpperCase(name.charAt(name.indexOf(" ") + 1)) + name.substring(name.indexOf(" ") + 2);
I'm not sure if that works %100 right, but it's a basic idea.
Re: Converting Cases Help
Use Substring() twice.
First character = Substring(0,1);
theRest= Substring (1,name.Length()-2);
I hope you prefer the teaching approach rather than the handed to you in a single line of code.
Re: Converting Cases Help
Bradshjo, If you have any questions about the methods I used feel free to ask me, or look at the Java String API doc here.
Scott, whenever someone gives me an answer I usually mess around with the code to see exactly how it works. I also check the Java documents on all the methods. I made the mistake and assumed that he would do the same. I'm sorry if my post offended you in any way.
Re: Converting Cases Help
I appreciate the apology but not needed. No offense is taken. Different people have different learning methods. If he doesn't dissect your answer or follow my advice he has no hope of making it through a programming course either way. My apologies in return as re-reading my post it sounds pretty snappy, which wasn't my intention.