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

    Default 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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!

  3. #3
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default

    i made a link to it

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Meta View Post
    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. :)

  5. #5
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default

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

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    First a nitpick regarding this:
    Java Code:
    while (wordToSearch != "stop")
    Never compare Strings with == or !=. Instead use either the equals(...) or equalsIgnoreCase(...) methods.


    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!

  7. #7
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default

    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?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Meta View Post
    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?
    Yes, that's exactly what it means. It's what I suggested above.

  9. #9
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default

    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?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Meta View Post
    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

    Java Code:
    public void translate(String[] sourceWords, String[] translatedWords) {
      //...
      for (int i = 0; i < sourceWords.length; i++) {
        //...
     
    }
    here sourceWords can represent englishArray or spanishArray, depending on the parameters that you pass into this method when you call it.

    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.

  11. #11
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default

    wat do you mean, abbreviations. which ones did i use? do you mean "u" and "i" "wont"

    thanks for help too

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.

  13. #13
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default

    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

  1. Replies: 1
    Last Post: 03-04-2010, 11:19 AM
  2. Red5 0.7 code to 0.9 rc2 code
    By gerd33 in forum New To Java
    Replies: 4
    Last Post: 12-29-2009, 07:50 PM
  3. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 11:14 AM
  4. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 01:21 PM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

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