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

Hey guys, I'm almost finished my assignment. It's purpose is to translate english words to pirate language. For example, when some enters "hello", "ahoy" is returned. But when I enter two words like "pardon me", "null" is returned even though I have assigned "avast" to it. I'm thinking this is because I have included Console.readToken() instead of readString(). But token in my opinion works better. So I'm not sure what I can do for more than one word like "pardon me" or "excuse me". Can anyone help? Here is the code: http://pastebin.com/q3EY40eu

OpenStudy (anonymous):

I use scanner for reading inputs and you can assign results to arrays

OpenStudy (anonymous):

I'm not sure I understand what you mean by assigning results to arrays? Is there a big difference in using scanner? We've never used it before...

OpenStudy (anonymous):

idk, i have never used console.readtoken :D i believe you can do same too Also when you import something don't use wildcard(*), it's bad practise, because you may import something what you don't eat and create same method in your program and then it may do weird stuff. And also it's much clearer for other who read your code, to see what class you going to use from library. if you need only console write: import java.util.Console

OpenStudy (anonymous):

we haven't learnt abou hashes so I don't really know what you doing here :D

OpenStudy (anonymous):

Oh ok that's alright and thanks for the import tip :)

OpenStudy (anonymous):

also I get errors when compiling your code: PirateTranslator.java:35: cannot find symbol symbol : variable Console location: class PirateTranslator while(!Console.endOfFile()) ^ PirateTranslator.java:38: cannot find symbol symbol : variable Console location: class PirateTranslator String input = (String) hashmap.get(Console.readToken()); ^ 2 errors I wonder why it's happen for me? I am pretty sure it works for you since you made it :D

OpenStudy (anonymous):

I think you need to download the Console file to your working java directory. I've attached the one I use if you want to use it :)

OpenStudy (anonymous):

http://docs.oracle.com/javase/6/docs/api/java/io/Console.html ah it's in java.io.Console however there are no such methods as endOfFile or readToken

OpenStudy (anonymous):

so you using custom library, so scanner better because it's built-in already :P

OpenStudy (anonymous):

what is token?

OpenStudy (anonymous):

Token takes in one word at a time rather than a whole sentence :) As for the other methods I used, I don't really know why there are no such methods :P

OpenStudy (anonymous):

and readString would read whole line?

OpenStudy (anonymous):

Yep :)

OpenStudy (anonymous):

http://pastebin.com/0AVkrQ8Z haven't tested but should work :P

OpenStudy (anonymous):

of course if you enter more than 100 strings it worn't work, but if you need it you can create new array with bigger size and copy all results

OpenStudy (anonymous):

I can see what you're doing. It compiles but it just keeps waiting for me to enter more data until the input size reaches over 100 where I get the out of bounds error. It doesn't return the pirate equivalent :/

OpenStudy (anonymous):

!Console.endOfFile() what does it mean?

OpenStudy (anonymous):

it should finish reading when you press enter or not?

OpenStudy (anonymous):

and you are not reading from file, are you?

OpenStudy (anonymous):

I press ctrl + z and close the program. I entered hi pardon me. It returned "yo-ho-ho" for hi but "null" for pardon me... !Console.endOfFile() means that while the input being entered has not reached the end, keep going or do whatever comes after that statement.

OpenStudy (anonymous):

so when input ends? when you close program? :D

OpenStudy (anonymous):

if you understand what I have done, you can rewrite it so it fits your requirements

OpenStudy (anonymous):

I'm using a txt file called pirate.txt and in it is written "hi madam how far is the pub" the output is "yo-ho-ho proud beauty null null be th' Skull & Scuppers null"

OpenStudy (anonymous):

Ok I'll go through it and see if I can figure it out :)

OpenStudy (anonymous):

then it should work, if you read from file

OpenStudy (anonymous):

while(!Console.endOfFile()) { input[noOfWords] = Console.readToken(); System.out.println(input[noOfWords]); noOfWords++; } // while make this and write here output

OpenStudy (anonymous):

I ran the while loop you just gave me and this is what I got: pardon me madam where is the hotel null null proud beauty whar be th' fleabag inn null

OpenStudy (anonymous):

so it reads null for some reason, just ignore last array

OpenStudy (anonymous):

can you do it yourself? :P

OpenStudy (anonymous):

here's code if you fail (i hope not) http://pastebin.com/t3kvrWai new version

OpenStudy (anonymous):

I ran your new version and again it returns null null for "pardon me". :[ I was thinking maybe I could just remove the me from "pardon me"? Then it won't return null as it will only read in one word i.e pardon?

OpenStudy (anonymous):

ah yeah it won't work because hashmap.put("pardon me", "avast"); it's 2 strings

OpenStudy (anonymous):

http://pastebin.com/LfRZxxCt try this solution

OpenStudy (anonymous):

if word is pardon or excuse it adds another word which ALWAYS will be me to same string and it will be able to translate yout got it?

OpenStudy (anonymous):

No because it still returning null null. I can't figure it out :S

OpenStudy (anonymous):

while(!Console.endOfFile()) { input[noOfWords] = Console.readToken(); if (input[noOfWords] == "pardon" || input[noOfWords] == "excuse") input[noOfWords] += Console.readToken(); System.out.println(input[noOfWords]); noOfWords++; } // while run this again and show me output :D

OpenStudy (anonymous):

Ok here it is: pardon me madam where is the hotel null null proud beauty whar be th' fleabag inn

OpenStudy (anonymous):

Just a guess, but I think pardon me is parsed as 2 separate strings, "pardon" and "me".

OpenStudy (anonymous):

yeah i noticed that

OpenStudy (anonymous):

and offered another solution

OpenStudy (anonymous):

since there are no "pardon" or "me" keys in the hashmap, it returns null :-P

OpenStudy (anonymous):

i tried to concatenate them but it still doesn't work

OpenStudy (anonymous):

That's what I noticed and thought I should change the key from "pardon me" to 2 seperate ones: "pardon" and "me" but we were given the phrases with some that have 1 word and some with 2 so I'm wary of doing that :/

OpenStudy (anonymous):

upload text file which you use here

OpenStudy (anonymous):

OpenStudy (anonymous):

and say how to read it? :D just type it's name in console?

OpenStudy (anonymous):

This is what I type in console: java PirateTranslator < pirate.txt

OpenStudy (anonymous):

ha i think know where mistake

OpenStudy (anonymous):

Really? :)

OpenStudy (anonymous):

Have it look for 'me' words, and then check to see what word precedes it. If it is either pardon or excuse, then do something unique for either case. Otherwise just proceed.

OpenStudy (anonymous):

agdwngo I already done it, but I made some kinda mistake with String manipulation

OpenStudy (anonymous):

http://pastebin.com/nn4TbgFp

OpenStudy (anonymous):

when you compare strings you need to use .equals("text") instead of ==

OpenStudy (anonymous):

you can read more about this here http://javatechniques.com/blog/string-equality-and-interning/

OpenStudy (anonymous):

Oh my goodness, thank you so so so so much!!! It works perfect! I can't believe I didn't realise about the .equals part lol we even did this in class (silly me :P) Thanks again so much and for your patience :D

OpenStudy (anonymous):

your welcome :) I always forget to do .equalsTo with string >< and too bad that most of the time it work's perfectly

OpenStudy (anonymous):

Yeah haha it's easy to forget sometimes :)

OpenStudy (anonymous):

in C++ , the equality operator is overloaded :-D

OpenStudy (anonymous):

what does it mean that it's overloaded?

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!