Results 1 to 2 of 2
  1. #1
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default 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(); 
    					}	
    			}			
    		}
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Add a "break" in the block where you set wordFound to true, maybe?

Similar Threads

  1. how to make my code better
    By vendetta in forum New To Java
    Replies: 4
    Last Post: 02-10-2010, 09:14 PM
  2. Code to make an Applet that downloads?
    By Leeky in forum Java Applets
    Replies: 0
    Last Post: 09-05-2009, 10:56 PM
  3. Replies: 4
    Last Post: 03-25-2009, 01:54 PM
  4. How to write efficient maintainable code.
    By Zosden in forum Advanced Java
    Replies: 9
    Last Post: 05-01-2008, 04:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •