Page 1 of 2 12 LastLast
Results 1 to 20 of 39
  1. #1
    yalondg is offline Member
    Join Date
    Jan 2011
    Posts
    20
    Rep Power
    0

    Exclamation English to "133t sp34k" translator

    here is my code, im trying to convert user input into "l33t sp34k" and I dont understand why its not working
    Java Code:
    import java.io.*;
    class translate
    {
    	public static void main(String[] args) throws IOException{
    
    		
    		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
                    System.out.println("Please enter what you want translated");  
                    String input;
                   input = reader.readLine();
              
                 String[] English={"e", "a","l","i"};
               
                 String[] l33t ={"3", "4","1",""}; 
                 
                for(int x=0;x<English.length;x++){
                	if(input.equals(English[x])){
                    	System.out.println(l33t[x]);
                			} //End of if..        
                		}//End of for statement       
    				} //End of class main
    }
    Last edited by yalondg; 01-22-2011 at 12:35 AM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Is there a question here?

    also, use code tags please [ code ] and [ /code ] around your code without spaces.

    A better way would be to create a helper method which takes a 1 letter string and returns a 1 letter string. It loops through the English words, comparing the 1 letter input to the English letter, if it finds it it should return the leet word, if it doesn't, return input.

    Then, prompt for word or words to translate, store it in a variable and loop through it applying the helper method, there are some flaws that may need to be worked out but I will let you figure them out, try also having an array of input.length().
    Last edited by sunde887; 01-22-2011 at 12:37 AM.

  3. #3
    yalondg is offline Member
    Join Date
    Jan 2011
    Posts
    20
    Rep Power
    0

    Default

    yes, what can a change to fix it

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I gave you some good spots to work from, I coded it and it works correctly, you will have to figure out changes necessary to complete it. Let me know when you implement what I suggested and what errors you run into.

    In your code it compares the full input to a single letter in the english array, if input is "hello" it performs
    Java Code:
    if("hello".equals(english[i]))
    which is an immediate problem, it will rarely return true. Also it only handles if the input if it does match the English item, if it does not match it just ends the program. If you split the input into 1 letter strings it would work like this: "are" = "43", "leet" = "133". Try what I mentioned above, don't be afraid to use smaller functions to break the problem up.

    One more point, try using a scanner, it might be easier
    Java Code:
    import java.util.Scanner;
    ...
    Scanner input = new Scanner(System.in);
    Last edited by sunde887; 01-22-2011 at 12:58 AM.

  5. #5
    yalondg is offline Member
    Join Date
    Jan 2011
    Posts
    20
    Rep Power
    0

    Default

    thanks, i didnt see the responce

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I like to edit my posts a lot, I apologize. With the information I have added to my two posts prior to this you should easily be able to solve this, let me know if you figure it out, good luck!

  7. #7
    yalondg is offline Member
    Join Date
    Jan 2011
    Posts
    20
    Rep Power
    0

    Default

    i fixed some of the things you told me were porblems, and it now seems like there is only one thing preventing it from working... is there anything i could do to be able to do this
    Java Code:
     inputArray[x]
    while "x" is like x used in algebra problems?

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    x is equal to the index of the array
    Java Code:
    if x = 0
    arr[x] = arr[0]
    x = 1
    arr[x] = arr[1]
    x = n
    arr[x] = arr[n]

  9. #9
    yalondg is offline Member
    Join Date
    Jan 2011
    Posts
    20
    Rep Power
    0

    Default

    here, ive made some of the changes you suggested... how else can I make it work?
    Java Code:
    		import java.io.*;
    	class translator{ 
    	
    		public static void main(String[] args) throws IOException{}
    			
    			
    			BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    			String input;
    			char[] inputArray;{
    			try{ System.out.println("Please enter what you want translated");  
    	                
    	               
    					input = reader.readLine();
    			
    	                	
    	                	inputArray = input.toCharArray();
    	               }
    	               catch(IOException ioe){
    	            	   System.out.println("ERROR #971823 has occurred");
    	               }
    	               inputArray = input.toCharArray();
    	             char[] English={'e', 'a','l','i'};
    	           
    	             String [] l33t ={"3", "4","1",""}; 
    	            
    	                          
    	            for(int x=0;x<English.length;x++){
    	            	if(inputArray[0] == (English[0])){
    	                	System.out.println(l33t[0]);
    	            			}	//end of if...
    	            	else {
    	            		String inputArrayString = inputArray.toString();
    	            		System.out.println("" + inputArrayString);
    	            	}
    	            	// I will repeat the "if else" before this comment for
    	            	  // each character in each array for example, English 
    	            	 for(int y=0;y<English.length;y++){
    	 	            	if(inputArray[1] == (English[1])){
    	 	                	System.out.println(l33t[1]);
    	 	            			}	//end of if...
    	 	            	else {
    	 	            		String inputArrayString = inputArray.toString();
    	 	            		System.out.println("" + inputArrayString);
    	 	            	}
    	 	            	 for(int z=0;z<English.length;z++){
    	 		            	if(inputArray[2] == (English[2])){
    	 		                	System.out.println(l33t[2]);
    	 		            			}	//end of if...
    	 		            	else {
    	 		            		String inputArrayString = inputArray.toString();
    	 		            		System.out.println("" + inputArrayString);
    	 		            	}
    	 		            	 for(int a=0;a<English.length;a++){
    	 			            	if(inputArray[3] == (English[3])){
    	 			                	System.out.println(l33t[3]);
    	 			            			}	//end of if...
    	 			            	else {
    	 			            		String inputArrayString = inputArray.toString();
    	 			            		System.out.println("" + inputArrayString);
    	 			            	}
    	 	            	} //End of for       
    	            		}//End of main 
    	            	 }
    	            	 }
    	            }
    					} //End of translator
    Last edited by yalondg; 02-02-2011 at 04:47 AM.

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

    Default

    Quote Originally Posted by yalondg View Post
    here, ive made some of the changes you suggested... how else can I make it work?
    You may start by telling us how it's not working. Making us guess will only make us give you wrong answers. Also, you should do some debugging/detective work. Put println statements in your code to see what is happening at various locations.

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    It's extremely inefficient to create a loop for each item in the char array. Try using a nested loop.

    have the first loop loop through each item in the inputArray then for each item, have a second loop loop through the english array.

    Try it out and if you have trouble post your code and Ill give you some more help, you seem pretty close though.

  12. #12
    yalondg is offline Member
    Join Date
    Jan 2011
    Posts
    20
    Rep Power
    0

    Default

    when i tried to compile in in Terminal it said
    Java Code:
    Exception in thread "main" java.lang.NoClassDefFoundError: translator
    Caused by: java.lang.ClassNotFoundException: translator
    	at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:248)

  13. #13
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    show me the code please.

  14. #14
    yalondg is offline Member
    Join Date
    Jan 2011
    Posts
    20
    Rep Power
    0

    Default

    Java Code:
    	import java.io.*;
    	class translator{ 
    	
    		public static void main(String[] args) throws IOException{}
    			
    			
    			BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    			String input;
    			char[] inputArray;{
    			try{ System.out.println("Please enter what you want translated");  
    	                
    	               
    					input = reader.readLine();
    			
    	                	
    	                	inputArray = input.toCharArray();
    	               }
    	               catch(IOException ioe){
    	            	   System.out.println("ERROR #971823 has occurred");
    	               }
    	               inputArray = input.toCharArray();
    	             char[] English={'e', 'a','l','i'};
    	           
    	             String [] l33t ={"3", "4","1",""}; 
    	            
    	                          
    	            for(int x=0;x<English.length;x++){
    	            	if(inputArray[0] == (English[0])){
    	                	System.out.println(l33t[0]);
    	            			}	//end of if...
    	            	else {
    	            		String inputArrayString = inputArray.toString();
    	            		System.out.println("" + inputArrayString);
    	            	}
    	            	// I will repeat the "if else" before this comment for
    	            	  // each character in each array for example, English 
    	            	 for(int y=0;y<English.length;y++){
    	 	            	if(inputArray[1] == (English[1])){
    	 	                	System.out.println(l33t[1]);
    	 	            			}	//end of if...
    	 	            	else {
    	 	            		String inputArrayString = inputArray.toString();
    	 	            		System.out.println("" + inputArrayString);
    	 	            	}
    	 	            	 for(int z=0;z<English.length;z++){
    	 		            	if(inputArray[2] == (English[2])){
    	 		                	System.out.println(l33t[2]);
    	 		            			}	//end of if...
    	 		            	else {
    	 		            		String inputArrayString = inputArray.toString();
    	 		            		System.out.println("" + inputArrayString);
    	 		            	}
    	 		            	 for(int a=0;a<English.length;a++){
    	 			            	if(inputArray[3] == (English[3])){
    	 			                	System.out.println(l33t[3]);
    	 			            			}	//end of if...
    	 			            	else {
    	 			            		String inputArrayString = inputArray.toString();
    	 			            		System.out.println("" + inputArrayString);
    	 			            	}
    	 	            	} //End of for       
    	            		}//End of main 
    	            	 }
    	            	 }
    	            }
    					} //End of translator

  15. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Java Code:
    public static void main(String[] args) throws IOException{} <-- ahem!

  16. #16
    yalondg is offline Member
    Join Date
    Jan 2011
    Posts
    20
    Rep Power
    0

    Default

    yea i just noticed that... i fixed it and the program ran but when i type something in it repeats "[C@54fc9944"

  17. #17
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    That output is what you get from the toString method in the Object class. So somewhere in your class your are attempting to print an object that does not have it's own toString method. The JVm cannot read your mind as to what format the display will be. So it is up to you to determine that.

  18. #18
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The code is still a bit overcomplicated.

    Like stated above, you only need 2 loops, this also makes it more efficient if you decide to add new letters to english[] and leet[].

    You can also use str.substring so you wouldn't have to change the input to an array, then the array back to a string. Your code seems close but you can improve it a lot still.

    Instead of printing the leet version when you find it try appending to string, or stringbuilder object. if it matches append it, if it doesn't match, append the input letter.

    The code has some flaws and it can be improved drastically, I would suggest you try one of the improvements, it may be easier and faster to do it another way.

    the two ways I'd suggest is either using a nested loop, or a method to do the comparisons/swaps on a single letter string.
    Last edited by sunde887; 02-04-2011 at 03:14 AM.

  19. #19
    yalondg is offline Member
    Join Date
    Jan 2011
    Posts
    20
    Rep Power
    0

    Default

    how could i do something like this
    Java Code:
    int inputLength = input.length();
    ...
    input.substring(0,inputLength )

  20. #20
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    If you want to use substring, the best way would be a loop

    for example:
    Java Code:
    String s = "Hello";
    for(int i = 0; i < s.length() - 1; i++){
      System.out.printn(s.substring(i, i + 1));
    }
    This will print
    Java Code:
    H
    e
    l
    l
    o
    With a nested loop, you could have the first(outer) loop going through all the characters in the string input, then the inner loop checking and swapping the letters when necessary.

    When you first asked this question I solved it with a static method
    The static method had the following contract
    Java Code:
    public static String translate(String s)
    where s is ALWAYS a 1 letter string. From there I looped through the input string, calling translate on each item of the string and appending it to a new string.

    If you don't like using a static method however, you are pretty close with using loops

    Try like I said above
    Java Code:
    for(int i = 0; i < input.length(); i++){
      //extract each letter and set it to some variable
        for(int j = 0; j < english.length(); j++){
          //change english to leet where necessary and add it to result string
        }
    }
    Last edited by sunde887; 02-07-2011 at 06:10 AM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 3
    Last Post: 10-12-2010, 05:21 PM
  2. Replies: 1
    Last Post: 01-21-2010, 10:20 AM
  3. getDisplayLanguage returns "en" not "English"
    By DD70 in forum New To Java
    Replies: 6
    Last Post: 08-13-2009, 12:22 AM
  4. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Tags for this Thread

Posting Permissions

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