• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to do it?  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how would a program code look like, input ten words and output them backwards in a msdos environment. im really a noob at this, my code this far is:




thanks in advance
 
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

If this was my problem, I would probably break it down into three high level parts
1) Ask the user for 10 words
2) Store them somehow, maintaining the order in which they were provided
3) Once 10 words are provided, print them out in the reverse order.

If you agree to this, which part are you having trouble with?
For #1 I would suggest using the Scanner class
For #2 I would suggest using an appropriate class from the Collections framework
You already know #3 because
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again. I agree with Maneesh that a BufferedReader is not ideal for reading from System.in. Note that you are reading a line at a time; you may not get ten words if you read ten lines.
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
does "input ten words" preclude passing them in as arguments?

>java Baktext one two three four five six seven eight nine ten

then loop over the args array backwards.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
petri hakamaa wrote:how would a program code look like, input ten words and output them backwards in a msdos environment. im really a noob at this, my code this far is:

It'll probably come as no surprise to Campbell that I'm going to disagree with him and Maneesh slightly about using Scanner, because that's not the problem with your code. What you've written is absolutely correct for inputting lines - it's just not complete for the purposes of your problem.

The problem right now is that you don't check how many words your user types in; you assume they enter 10 words in one line.

For example, what if they type in:
The quick brown fox jumps over the lazy dog again and again
That's 12 words; and what about:
The quick brown fox jumps over the lazy dog
That's only 9.

So your problem is: How do you
(a) Only output the first (or possibly last) 10 words they type in.
(b) Keep asking for input until they've entered at least 10 words.

Now the fact is that counting words with a Scanner might be easier because they are geared for "word-like" input; but they also have several "gotchas" that you need to know about - especially for keyboard input.

So, a third alternative for you (assuming Fred's suggestion was #2): Have a look at the split() method in java.lang.String (←click), because that will split a line into "words" if you call it correctly, which means you can continue to use your BufferedReader if you want.

As you'll discover, there's no one "right" way to do things in programming.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And change the last line to print r rather than s.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!