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
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.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Joined: Oct 13, 2012
My teacher suggested using:
int pos = text.indexOf("");
String word = text.substring(pos+1);
pos= new Text.indexOf("");
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.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com