Forums Register Login

Help with a java string program please

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
also...

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.
+Pie Number of slices to send: Send
My teacher suggested using:

int pos = text.indexOf("");
String word = text.substring(pos+1);
pos= new Text.indexOf("");
Stringword=newText.substring(0,pos);
word.toUppercase

Does this make any sense?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

Ryan Hickman wrote: . . .

int pos = text.indexOf(""); . . .

You mean " " not "", surely?
+Pie Number of slices to send: Send
 

Ryan Hickman wrote:Does this make any sense?


Kind of; although you should read Campbell's last post.

If you're not familiar with split() yet, you might also want to look at String.indexOf(String, fromIndex), which is quite useful when used in a loop.

Winston
1
+Pie Number of slices to send: Send
the first thing you really need to do is STOP.

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?
+Pie Number of slices to send: Send
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.
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
https://gardener-gift.com


reply
reply
This thread has been viewed 959 times.
Similar Threads
Counting uppercase?
I am new and need help with this plzzzzzzzzzzz
palindrome checker, tried to debug, keeps looping why?
Scanner string parameters
Counting uppercase? Part 2!
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 04:34:43.