Results 1 to 13 of 13
Thread: Help with my code
- 03-17-2010, 01:09 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
Help with my code
I need some help with my code. it uses 2 arrays, one has english words, one has spanish words. i have to let the user decide which language to start in and it will translate.
NoMorePasting.com
i used if statements and the such in my code, but what i need to do is use methods instead. my teacher says i need to use a method and call that method 2x passing it which away to search. i can't just repeat the code to go both ways.
but im confused. can i get any help.
-
You may wish to post your code in here (but please don't forget to use code tags -- see my signature for details).
Much luck!
- 03-17-2010, 01:19 AM #3
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
i made a link to it
-
I saw that, but please realize that many here (myself included) are reluctant to click on links. But of course what you do is entirely up to you, and I think that it mostly depends on how quickly you desire help. If you're willing to wait for others who would click on the link, then there's no need to post your code here.
Regardless, I wish you much luck with this project. :)
- 03-17-2010, 01:37 AM #5
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
Java Code:import java.util.Scanner; public class Lab2 { public static void main(String[] args) { String[] EnglishArray = {"cat", "dog", "house", "table", "turtle", "water", "yellow", "zoo"}; String[] SpanishArray = {"gato", "perro", "casa", "mesa", "tortuga", "agua", "amarillo", "zoologico"}; Scanner keyboard = new Scanner(System.in); int languageSelect = 0; String wordToSearch = null; System.out.println("Would you like to start in English(Enter 1) or in Spanish(Enter 2)?"); //have the user enter their choice until it is valid. do { languageSelect = keyboard.nextInt(); if(languageSelect !=1 && languageSelect !=2) { System.out.println("Please Enter 1 for English or 2 for Spanish"); } }while(languageSelect !=1 && languageSelect !=2); //let the user enter words in English for translation if they type 1 if (languageSelect == 1) { System.out.println("Enter a word in English. Type 'stop' to end. "); wordToSearch = keyboard.next(); //user can keep entering words, until they type "stop" while (wordToSearch != "stop") { for (int i = 0; i< EnglishArray.length; i++) //goes through the whole array until it finds the word { if (wordToSearch.equalsIgnoreCase( EnglishArray[i] )) //ignoring upper case or lower case, finds the word { System.out.println("In Spanish " + wordToSearch + " is " + SpanishArray[i]); //uses the index it found the English word in the English array, and uses the Spanish word in the same index of the Spanish array System.out.println("Enter your next word."); wordToSearch = keyboard.next(); } } } System.exit(0); } //let the user enter Spanish words for translation if they type 2 else if (languageSelect == 2) { //user can keep entering words, until they type "stop" System.out.println("Enter a word in Spanish. Type 'stop' to end. "); wordToSearch = keyboard.next(); while (wordToSearch != "stop") { for (int i = 0; i< SpanishArray.length; i++) //goes through the whole array until it finds the word { if (wordToSearch.equalsIgnoreCase( SpanishArray[i] )) //ignoring upper case or lower case, finds the word { System.out.println("In English " + wordToSearch + " is " + EnglishArray[i]); //uses the index it found the Spanish word in the Spanish array, and uses the English word in the same index of the English array System.out.println("Enter your next word."); wordToSearch = keyboard.next(); } } } System.exit(0); } } }
-
First a nitpick regarding this:
Never compare Strings with == or !=. Instead use either the equals(...) or equalsIgnoreCase(...) methods.Java Code:while (wordToSearch != "stop")
You have two section of code that are almost exactly alike, and here's one of them:
Java Code:while (wordToSearch != "stop") { // **** fix this line as noted above **** for (int i = 0; i < EnglishArray.length; i++) { if (wordToSearch.equalsIgnoreCase(EnglishArray[i])) { System.out.println("In Spanish " + wordToSearch + " is " + SpanishArray[i]); // **** you might want to get this outside of the for loop **** // **** else your user will be stuck if they enter a word not in the source array **** System.out.println("Enter your next word."); wordToSearch = keyboard.next(); } } }
The only thing different about the sections are
1) Which array to search for the word to translate,
2) Which array extract the translation
3) A single phrase, "In Spanish" vs. "In "English"
So first you know that your method will have a block similar to this in it. All you need to do is pass the arrays in proper order. A possible method signature could be:
Java Code:public static void translate(String[] sourceWords, String[] translatedWords[], String translatedLanguage) { // TODO: use the code above to flesh out a method. :) }
and would be called like so:
Java Code:translate(englishArray, spanishArray, "Spanish");
Keep on plugging!
- 03-17-2010, 02:13 AM #7
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
teacher says you can't just repeat the code to go both ways. You need to use a method and call that method 2x passing it which away to search.
not sure wat that means. do u think it means i only have 1 method, and depending on how i pass the arrays it will search opposite ways?
-
- 03-17-2010, 02:24 AM #9
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
but if the code is used only one time in the method body, wont the for loop that is doing i < englisharray.length stay the same? like it will say englisharray, but what if it actually means the spanisharray. u no what i mean?
-
That's where the magic of parameters comes in. You don't hard-code englishArray or spanishArray into your method, but rather use parameter variables
here sourceWords can represent englishArray or spanishArray, depending on the parameters that you pass into this method when you call it.Java Code:public void translate(String[] sourceWords, String[] translatedWords) { //... for (int i = 0; i < sourceWords.length; i++) { //... }
One other request: please avoid using non-standard abbreviations here. For many here English is their second or third language, and these non-standard abbreviations make it hard for many to understand what you're posting. Thanks in advance.
- 03-17-2010, 02:44 AM #11
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
wat do you mean, abbreviations. which ones did i use? do you mean "u" and "i" "wont"
thanks for help too
-
Yes, ... and "no" for "know" and "wat" for "what". If you do any programming at all, you'll know that any little mistake can be deadly. So if you want to communicate your question here, it's best to be as clear as possible so we and you won't propagate mistakes.
- 03-17-2010, 04:40 AM #13
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
ok. i think i made it pretty far with my code. the one thing im having trouble figuring out is. if they enter a word that is NOT found, i need to display that it was not found, allow them to enter a new word, and have it go thru the process again. but im not sure where to insert that. heres my new code:
Java Code:import java.util.Scanner; public class LabToo { public static void main(String[] args) { String[] englishArray = {"cat", "dog", "house", "table", "turtle", "water", "yellow", "zoo"}; String[] spanishArray = {"gato", "perro", "casa", "mesa", "tortuga", "agua", "amarillo", "zoologico"}; Scanner keyboard = new Scanner(System.in); int languageSelect = 0; String wordToSearch = null; System.out.println("Would you like to start in English(Enter 1) or in Spanish(Enter 2)?"); do { languageSelect = keyboard.nextInt(); if(languageSelect !=1 && languageSelect !=2) { System.out.println("Please Enter 1 for English or 2 for Spanish"); } }while(languageSelect !=1 && languageSelect !=2); if (languageSelect == 1) { System.out.println("Enter a word in English. Type 'stop' to end."); wordToSearch = keyboard.next(); translate(englishArray, spanishArray, wordToSearch); } else if (languageSelect ==2) { System.out.println("Enter a word in Spanish. Type 'stop' to end."); wordToSearch = keyboard.next(); translate(spanishArray, englishArray, wordToSearch); } } public static void translate(String[] sourceWords, String[] translatedWords, String translatedLanguage) { Scanner keyboard = new Scanner(System.in); while (! translatedLanguage.equalsIgnoreCase ("stop")) { for (int i = 0; i < sourceWords.length; i++) { if (translatedLanguage.equalsIgnoreCase(sourceWords[i])) { System.out.println("Translated, the word, " + translatedLanguage + " is " + translatedWords[i]); System.out.println("Enter your next word."); translatedLanguage = keyboard.next(); } } } } }
Similar Threads
-
can any one pls send me a sample code for calling a jsp code in swings
By sniffer139 in forum AWT / SwingReplies: 1Last Post: 03-04-2010, 11:19 AM -
Red5 0.7 code to 0.9 rc2 code
By gerd33 in forum New To JavaReplies: 4Last Post: 12-29-2009, 07:50 PM -
Convert java code to midlet code
By coldvoice05 in forum New To JavaReplies: 1Last Post: 08-12-2009, 11:14 AM -
Convert java code to midlet code
By coldvoice05 in forum Advanced JavaReplies: 1Last Post: 08-09-2009, 01:21 PM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks