Results 1 to 2 of 2
- 03-22-2010, 04:27 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
Need some help with code. Make it more efficient.
I have code and when it gets into the method it does pretty much the bulk of the code. Searches for a word and if it is found it will display it. my teacher says it is inefficient in that if found, it will stay in the FOR loop and keep searching. She wants me to return it to main for the repetition.
Im confused on how to do this, is it a quick and simple thing. this is due soon and i need to get this finished.
Java Code:import java.util.Scanner; public class LabToo { public static void main(String[] args) { String[] englishArray = {"book", "cat", "dog", "house", "school", "table", "turtle", "water", "yellow", "zoo"}; String[] spanishArray = {"libro", "gato", "perro", "casa", "escuela", "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); if (languageSelect == 1) { System.out.println("Enter a word in English. Type 'stop' to end."); wordToSearch = keyboard.next(); translate(englishArray, spanishArray, wordToSearch);//Calls a method to translate the user inputted word into the opposite language //Sends englishArray words as the source words, the spanishArray words as the outputted words, and the wordTosearch is what they typed } else if (languageSelect ==2) { System.out.println("Enter a word in Spanish. Type 'stop' to end."); wordToSearch = keyboard.next(); translate(spanishArray, englishArray, wordToSearch);//Calls the same method as above to translate the user inputted word into the opposite language //Sends spanishArray words as the source words, the englishArray words as the outputted words, and the wordTosearch is what they typed } } public static void translate(String[] sourceWords, String[] translatedWords, String translatedLanguage)//sourceWords is the original language, translatedWords is the new language, and translatedLanguage is the outputted word { Scanner keyboard = new Scanner(System.in); while (! translatedLanguage.equalsIgnoreCase ("stop"))//As long as the user does not type "stop" the user can keep entering words { boolean wordFound = false; for (int i = 0; i < sourceWords.length; i++) { if (translatedLanguage.equalsIgnoreCase(sourceWords[i])) { wordFound = true; System.out.println("Translated, the word, " + translatedLanguage + " is " + translatedWords[i]); } } if (wordFound) { System.out.println("Enter your next word."); translatedLanguage = keyboard.next(); } else { System.out.println("The word was not found. Enter another word."); translatedLanguage = keyboard.next(); } } } }
- 03-22-2010, 08:21 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Similar Threads
-
how to make my code better
By vendetta in forum New To JavaReplies: 4Last Post: 02-10-2010, 09:14 PM -
Code to make an Applet that downloads?
By Leeky in forum Java AppletsReplies: 0Last Post: 09-05-2009, 10:56 PM -
Eclipse don't take on consideration the code chages I make!
By karim in forum EclipseReplies: 4Last Post: 03-25-2009, 01:54 PM -
How to write efficient maintainable code.
By Zosden in forum Advanced JavaReplies: 9Last Post: 05-01-2008, 04:48 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks