This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes not returning entire sentence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "not returning entire sentence" Watch "not returning entire sentence" New topic
Author

not returning entire sentence

Mack fitz
Greenhorn

Joined: Jul 28, 2011
Posts: 3
Hi,

First let me say that I am not a programmer or a student, just someone that dabbles with programs in their spare time. So please forgive me if my terminology is a bit off. I am trying to a simple slang translator using an array that I can place inside an applet later. I have it so it translates the word that is in the list but it leaves the rest of the sentence out. If anyone can point me in the right direction to achieve getting the full sentence including the words that get translated I would greatly appreciate it. here is what I have already. thanks for any help in advance.
James E Baker
Greenhorn

Joined: Jul 28, 2011
Posts: 23
Mack fitz wrote:Hi,

If anyone can point me in the right direction to achieve getting the full sentence including the words that get translated I would greatly appreciate it. here is what I have already. thanks for any help in advance.

}


Hey Mack,

I'm not sure exactly what you're looking for. Could you give me an example of how you'd like the output to look? Right now, it looks like you'd get output like this:

convert it to slang
yes
awe yeah
Mack fitz
Greenhorn

Joined: Jul 28, 2011
Posts: 3
James,

Thanks for your reply.
When someone enters a complete sentence such as: If you want my opinion java can be hard. it only returns "two cents" instead of: If you want my two cents java can be hard.
Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
When someone enters a complete sentence such as: If you want my opinion java can be hard. it only returns "two cents" instead of: If you want my two cents java can be hard.


Thats because you are only printing out the content of that array index. What you need is an else to handle the words that do not match any in the translate list.
Remember you want to print something in each passing of the outer loop, the word in input or the translated one in translateList (hint: use System.out.print instead of System.out.println)
Another approach is to reconstruct the string by concatenating the words to a new string and print it out as whole after the loop
Mack fitz
Greenhorn

Joined: Jul 28, 2011
Posts: 3
Thanks people... Ive got it all squared away... I guess we can mark it up to a brain fart..Ive done the same program using a hashmap and the old if then statement but figured I would try this since I wasn't to sure about it.

Out of curiosity In your professional opinions which way is more efficient.. or a more correct version of the three..hashmap, this array, or if/then.
Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
Mack fitz wrote:Out of curiosity In your professional opinions which way is more efficient.. or a more correct version of the three..hashmap, this array, or if/then.


It simply depends on what you need, arrays are great for numbered indexes and when you don't expect the array size to change. Hashmap is used when you need associative array with Object keys that maps to one value (in you instance I'd recommend the hashmap)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
... and welcome to the Ranch
 
Consider Paul's rocket mass heater.
 
subject: not returning entire sentence
 
Similar Threads
Getting rid of redundancy?
execution program
Word count problem.
Alpha beta pruning with negamax
Using Nested For Loops To Make Triangle