File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Help with a java string program please Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with a java string program please" Watch "Help with a java string program please" New topic
Author

Help with a java string program please

Ryan Hickman
Ranch Hand

Joined: Oct 13, 2012
Posts: 36
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.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2315
    
  49
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.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11406
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ryan Hickman
Ranch Hand

Joined: Oct 13, 2012
Posts: 36
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?
Aj Prieto
Ranch Hand

Joined: Sep 28, 2012
Posts: 75

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.


Da mihi sis bubulae frustum assae, solana tuberosa in modo Gallico fricta ac quassum lactatum coagulatum crassum.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39395
    
  28
Ryan Hickman wrote: . . .

int pos = text.indexOf(""); . . .
You mean " " not "", surely?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11406
    
  16

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?
manasa manogna
Greenhorn

Joined: Mar 29, 2011
Posts: 22
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with a java string program please