• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with a java string program please

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ryan Hickman
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 75
Android Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ryan Hickman wrote: . . .

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

You mean " " not "", surely?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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'm so happy! And I wish to make this tiny ad happy too:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic