I am taking my first java class and the teacher wants us to write a program that will have the user enter their favorite quote and then in return the program will display: their quote, the length of their quote, the first word of their quote in uppercase, the first letter of the last word of their quote in uppercase, and display the first, middle, and last word of their quote labeled as such, as well as replacing every vowel with #.
I can do everything but: detect the first word of the user's quote and make it uppercase, detect the last word and make the first letter of the last word uppercase, and display the first, middle, and last word and label them as such.
The only thing I can think of right now is to restrict the user's quote to three words and make three different string variables.
First write out in detail how you would solve the problem if you had to do it. Then look at the available String methods to see how you can put your description into code.
I'm not sure what level you are at or what assumptions you can make but you may want to look at using the split() method.
I would say that each and every one of these tasks should be handled in separate methods. So you would have:
a method to get the string from the user
a method to compute the length of a string
a method to find the first word in a string (for some definition of "word")
a method to find the last word in a string
a method that capitalizes a string
...etc
once you do this, you start to see how they can fit together. For example, it doesn't matter where you get the string from when you go to capitalize it. Nor does it matter if it is one word or twenty words...you pass in a string and you get it (or a version of it) capitalized.
By separating out the tasks, you greatly simply what you need to do.
That sure is a lot of work just manipulate the first word.
Like Tony said, you should look into the split() method in the String class. It'll return a string array, and with that, you could easily get the first, middle, and last words.
Then you can do whatever you have to do to each word.
figure out how you identify words. WHAT defines where one word begins and ends. Don't think about java methods. Don't thing about Strings, data structures, arguments...nothing related to writing code.
If all you had was pencil and paper, and you had to tell a 10 year old child to figure out what all the individual words were, what would you tell them to do?
In this case, you can use split method. One more alternative is to use the Scanner and it's splitting functionality where you can specify the delimiters. Using scanner, you can easily read in the text you want one line at a time and then split it. Next, you can read each word and store it in a String array. Now you are ready to play with the words so that perform any operation of your wish.
Post by:autobot
joke time: What is brown and sticky? ... ... ... A stick! Use it to beat this tiny ad!
a bit of art, as a gift, that will fit in a stocking