Ask your own question, for FREE!
Computer Science 7 Online
OpenStudy (anonymous):

I have an assignment where I have to translate normal phrases to pirate language. I created 2 arrays to store normal phrases and the translated text. I'm not sure this is right. I'm not expecting anyone to do it for me but can anyone help just to point me in the right direction? I'm using io redirection. The code I have so far and the input file I'm using can be found here: http://pastebin.com/P26i9qWb

OpenStudy (anonymous):

Right, you can use two arrays, you can use a number of data structures actually. So lets just say you decide to stick with arrays... The process could be to loop through the file line by line, read the tokens (or even split the line that's read by comma), put the first chunk into one array, the second chunk into the other. Then look at whatever word is given to the program and snag it from the array. So the first thing to tackle would be to load the arrays. Once you get that working, write a procedure possibly to loop through the arrays and match the given word to the 'english' word entry, then do a translation. This only works if the arrays are aligned since you're matching by array index. Another option would be to use a 'Dictionary', although I am not sure that this assignment permits the use of it (you be the judge).

OpenStudy (anonymous):

I was thinking to do that as we have done lab exercises where we were required to search an array and return the index of a particular variable. I have split the words into 2 arrays, english and pirate. I'll start working on it now, thanks for your help :)

OpenStudy (anonymous):

No problem, my answer is no means a final answer, so post more if you have any questions and we'll help you out! Arrays can be a hassle at times if you let them get away from you! Good luck matey!

OpenStudy (anonymous):

Will do haha thanks alot!

OpenStudy (anonymous):

you can also create array of objects in which there would be english and pirate phrase

OpenStudy (anonymous):

I've come up with this so far. The search method makes sense to me but it's obvious there's a problem with it as it does not return the required index. For example, when I write "hello" I expect it to return 0 but I always seem to get 12 returned instead. Is my method wrong? http://pastebin.com/tdMWi329

OpenStudy (anonymous):

you should have your array sorted already if you use this algorithm to find word in an array, simple you could just scan through whole array from 0 to a.length-1

OpenStudy (anonymous):

you can use sort() and binarySearch() functions if don't want to write code yourself :D

OpenStudy (rsmith6559):

Be very careful with any sort routine. The english array will sort differently than the the pirate array. Most sorts will sort, they won't just give you an index to change so that you can move around BOTH arrays at the same time. This is a perfect dataset for a dictionary, map whatever Java calls a name:value pair.

OpenStudy (anonymous):

i believe if you used object then everything would be sorted correctly

OpenStudy (anonymous):

hi

OpenStudy (anonymous):

this is not a chat :/

OpenStudy (anonymous):

Sorry I haven't been on in a while just had a lot of things happening. I have used hashmap for my program. If I input "hello" it returns "ahoy" if I put in "madam" it returns "proud beauty" so it kinda works. But, if I input hello madam it returns "null". I'm thinking this is because I have only mapped hello to ahoy and madam to proud beauty. I haven't used arrays in this yet because I'm unsure of how to use it with hashmap? This is my code so far: http://pastebin.com/4yzUezJm

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!