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

    Default

    Thanks sunde887! i (think i) completed your instructions however, however whenever i try to use the variable "input" gets the error "The local variable input may not have been initialized"
    heres my code maybe it will help
    Java Code:
    	import java.io.*;
    	public class translator{ 
    	
    		public static void main(String[] args) throws IOException{
    		
    		
    			BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));//create reader
    			String input;//define input
    			
    			try{ 
    				System.out.println("Please enter what you want translated"); //Ask user for text to be inputed 
    	              
    				input = reader.readLine();//define String input as what the user 
    				int inputLength = input.length();
    				
    				
    				char[] inputArray;			//define inputArray
    	            inputArray = input.toCharArray();//define array of chars (s) as the input in char Array form
    	             String inputArray2 = inputArray.toString();
    			
    			
    			
    			
    			}
    	               catch(IOException ioe){//catch a IOException(if user types invalid input
    	            	   System.out.println("ERROR #971823 has occurred");
    	               }
    	               char[] inputArray = input.toCharArray();
    	             //  inputArray = input.toCharArray();//re-define inputArray
    	             char[] English={'e', 'a','l','i'};//create array called English and insert some letters
    	           
    	             String [] l33t ={"3", "4","1","ˇ"}; 
    	            //your are attempting to print an object that does not have it's own toString method
    	               
    	             for(int i = 0; i < input.length(); i++){
    	            	  //extract each letter and set it to some variable
    	            	 String inputArray2 = inputArray.toString();
    	            	 /*	 ˇˇOLD VERSION!!
    	            	  * String a = inputArray[0],
    	            	 		   b = inputArray[1],
    	            	 	       c = inputArray[2],
    	            	 	       d = inputArray[3],
    	            	 		   e = inputArray[4],
    	            	 		   f = inputArray[5],
    	            	 		   g = inputArray[6],
    	            	 		   h = inputArray[7],
    	            	 		   k = inputArray[8],
    	            	 		   l = inputArray[9],
    	            	 		   m = inputArray[10];
    	            	  				*/
    	            	 
    	            	 	String a = inputArray2.substring(0,1),
    	            	 		   b = inputArray2.substring(1,2),
    	            	 	       c = inputArray2.substring(2,3),
    	            	 	       d = inputArray2.substring(3,4),
    	            	 		   e = inputArray2.substring(6,7),
    	            	 		   f = inputArray2.substring(8,9),
    	            	 		   g = inputArray2.substring(9,10),
    	            	 		   h = inputArray2.substring(10,11),
    	            	 		   k = inputArray2.substring(11,12),
    	            	 		   l = inputArray2.substring(12,13),
    	            	 		   m = inputArray2.substring(12,14);
    	            	 			for(int j = 0; j < English.length; j++){
    	            	      //change english to leet where necessary and add it to result string
    	            	 		int inputLength = input.length();
    	            	 		
    	            	 		inputArray = input.toCharArray();//define array of chars (s) as the input in char Array form
    	            	 		// String inputArray2 = inputArray.toString();
    	       			
    	            	 		 
    	            	 		String englishString = English.toString();
    	            	 		if (inputLength > j){
    	            	 			if (inputArray2.substring(j) == englishString.substring(j)){
    	            	 				
    	            	 			}
    	            	 			
    	            	 		}
    	            	    }
    	            	}
    	         
    	            		}//End of main 
    			
    	            	 
    		
    			
    					} //End of translator
    p.s im sorry if im fustrating you guys! im only 13 and its my first year of java!

  2. #22
    time2die is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default

    String[] English={"e", "a","l","i"};
    char[] l33t ={'3', '4','1','ˇ'};

    String english = in.readLine() ;
    String leet = null ;
    StringBuffer buff = new StringBffer(english);

    for(int i ; i < English.length ; i++)
    ////while( buff.indexOf(English[i]) != -1 )
    ////////buff.setChartAt( buff.indexOf(English[i]) , l33t[i])

    leet = buff.toString() ;
    you can do somethink like that
    Last edited by time2die; 02-11-2011 at 12:04 PM.

  3. #23
    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 don't mind helping, you have a lot of unnecessary code. Try just storing the input as a string and then leaving it.

    when you get to the loop you don't want to manually split the string because it can be different sizes. You want to use i to extract 1 letter at a time.
    Java Code:
    for(int i = 0; i < str.length(); i++){
      String s = str.substring(i , i + 1);
        //new loop
    This will loop through the string 1 item at a time, so you extract that 1 letter and do the comparison on it in the second loop.

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

    Default

    like this?(p.s sorry the formatting got screwed up from posting it)
    Java Code:
     
    	             //FIRST "FOR"
    	             for(int i = 0; i < input.length(); i++){
    	            	  String s = input.substring(i , i + 1);
    	            	  			
    	            	  
    	            	  //2ND "FOR"
    	            	  		for(int j = 0; j < English.length; j++){
    	      //change english to leet where necessary and add it to result string
    	            	  	int inputLength = input.length();
    	            	  					
    	            	  					
    		if (s == English[j]){
    	            	  							               System.out.println("" + s); 
    	            	  						}
    	            	 else{
    	            	  							System.out.println(input.substring(i , i + 1));
    	            	  						}

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

    Default

    You are close, however, you seem to have forgotten something, you tested for (s == english) and just print s, didn't you want to translate it?

    Also,when you are done I can post/message you the code I had written when you originally asked this if you would like to see another approach.

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

    Default

    Java Code:
    	for(int j = 0; j < English.length; j++){
    	            	  					//change english to leet where necessary and add it to result string
    	            	  					int inputLength = input.length();
    	            	  					
    	            	  					
    	            	  						if (s == English[j]){
    	            	  							s = l33t[j];
    	            	  							System.out.println("" + s);
    	            	  						}
    	            	  						else{
    	            	  							System.out.println(input.substring(i , i + 1));
    	            	  						}

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

    Default

    Did you compile it and run it to test? What happens? You can also make this create a string, then print the completed string.
    declare
    Java Code:
    String str = "";
    Then append each item to this new string. If the str is a word to be converted, append the leet symbol to this string, else append the regular letter to it. Alternately, it's probably better to use a StringBuilder, but I would suggest with sticking with strings since you are still fairly new.

    You are close, post your entire code and try compiling it and testing it and seeing what happens. Once you have completed code posted which you tested and is working let me know if you wanna see another approach.

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

    Default

    i couldnt compile if becuase of this error "The local variable input may not have been initialized" that i get whenever i use "input". i suspect it might be becuase input was initialized within a try block...so i dont know how i can fix that error without causing other ones... heres my code anyway
    Java Code:
    	import java.io.*;
    	public class translator{ 
    	
    		public static void main(String[] args) throws IOException{
    		
    		
    			BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));//create reader
    			String input;//define input
    			
    			try{ 
    				System.out.println("Please enter what you want translated"); //Ask user for text to be inputed 
    	              
    				input = reader.readLine();//define String input as what the user 
    				int inputLength = input.length();
    				
    				
    				
    				char[] inputArray;			//define inputArray
    	            inputArray = input.toCharArray();//define array of chars (s) as the input in char Array form
    	             
    			
    			
    			
    			
    				}
    	               catch(IOException ioe){//catch a IOException(if user types invalid input
    	            	   System.out.println("ERROR #971823 has occurred");
    	               }
    	               char[] inputArray = input.toCharArray();
    	             //  inputArray = input.toCharArray();//re-define inputArray
    	             char[] English={'e', 'a','l','i'};//create array called English and insert some letters
    	           
    	             String [] l33t ={"3", "4","1","ˇ"}; 
    	            //your are attempting to print an object that does not have it's own toString method
    	            
    	             //FIRST "FOR"
    	             for(int i = 0; i < input.length(); i++){
    	            	  String s = input.substring(i , i + 1);
    	            	  			
    	            	  
    	            	  //2ND "FOR"
    	            	  		for(int j = 0; j < English.length; j++){
    	            	  					//change english to leet where necessary and add it to result string
    	            	  					int inputLength = input.length();
    	            	  					char u = s.charAt(0);
    	            	  						
    	            	  						if (u == English[j]){
    	            	  							s = l33t[j];
    	            	  							System.out.println("" + s);
    	            	  						}
    	            	  						else{
    	            	  							System.out.println(input.substring(i , i + 1));
    	            	  						}
    	            	  						
    	            	  					
    	            	  					inputArray = input.toCharArray();//define array of chars (s) as the input in char Array form
    	            	 		// String inputArray2 = inputArray.toString();
    	       			
    	            	 		 
    	            	 		
    	            	 			
    	            	 	
    	            	    }
    	            	}
    	         
    	            		}//End of main 
    			
    	            	 
    		
    			
    					} //End of translator

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

    Default

    after buffered reader simply change from String input; to String input = "";

    Then try and compile it.

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

    Default

    when i type "hey" into the program and it returns
    Java Code:
     
    h
    h
    h
    h
    3
    e
    e
    e
    y
    y
    y
    y

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

    Default

    Try switching from System.out.println() to System.out.print(), then do it, post the answer, also, explain to me why you think its wrong if it is wrong, you seem pretty close.

    Also, it may be easier to not print the item when it finds something, and instead append it to some new string.

    at the top declare a string to
    Java Code:
    String result = "";
    Then append to that string and after the loops print the result.
    If you know how to use stringbuilder thats more efficient.

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

    Default

    i got
    Java Code:
    hhhh3eeeyyyy
    its wrong because it seems to print the translation based on what number of character in the array ( for example "a" is the second character in both English[] and l33t[] so whenever a is needed to be translated it prints aaa4) i think it might be because it either goes through loop 4 times and translates once...no idea how to fix it though :\

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

    Default

    You have some unneccesary code
    Java Code:
    for(int i = 0; i < input.length(); i++){
    	            	  String s = input.substring(i , i + 1);
    	            	  			
    	            	  
    	            	  //2ND "FOR"
    	            	  		for(int j = 0; j < English.length; j++){
    	            	  					//change english to leet where necessary and add it to result string
    	            	  					int inputLength = input.length();
    	            	  					char u = s.charAt(0);
    	            	  						
    	            	  						if (u == English[j]){
    	            	  							s = l33t[j];
    	            	  							System.out.println("" + s);
    	            	  						}
    	            	  						else{
    	            	  							System.out.println(input.substring(i , i + 1));
    	            	  						}
    	            	  						
    	            	  					
    	            	  					inputArray = input.toCharArray();//define array of chars (s) as the input in char Array form
    	            	 		// String inputArray2 = inputArray.toString();
    	       			
    	            	 		 
    	            	 		
    	            	 			
    	            	 	
    	            	    }
    	            	}
    what is inputArray? You aren't using it anywhere that I can see so feel free to remove that.where you define int inputLength in the second loop, you don't need that either. Finally, instead of char u = s.charAt(0) you can do
    Java Code:
    if(s.equals(english[j])){
      System.out.print(l33t[j]);
    }
    else{
      System.out.print(s);
    }
    The first loops goal is to break the input string into 1 item strings. The second loop is to either swap the english letter to l33t, or print the item.

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

    Default

    it just prints hhhheeeeyyyy when i made your corrections... heres my entire code maybe i changed it since last post
    Java Code:
    	import java.io.*;
    	public class translator{ 
    	
    		public static void main(String[] args) throws IOException{
    		
    		
    			BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));//create reader
    			String input = "";//define input
    			
    			try{ 
    				System.out.println("Please enter what you want translated"); //Ask user for text to be inputed 
    	              
    				input = reader.readLine();//define String input as what the user 
    				int inputLength = input.length();
    				
    				
    				
    				//char[] inputArray;			//define inputArray
    	            //inputArray = input.toCharArray();//define array of chars (s) as the input in char Array form
    	             
    			
    			
    			
    			
    				}
    	               catch(IOException ioe){//catch a IOException(if user types invalid input
    	            	   System.out.println("ERROR #971823 has occurred");
    	              }
    	              // char[] inputArray = input.toCharArray();
    	             //  inputArray = input.toCharArray();//re-define inputArray
    	             char[] English={'e', 'a','l','i'};//create array called English and insert some letters
    	           
    	             String [] l33t ={"3", "4","1","ˇ"}; 
    	            //your are attempting to print an object that does not have it's own toString method
    	            
    	             //FIRST "FOR"
    	             for(int i = 0; i < input.length(); i++){
    	            	  String s = input.substring(i , i + 1);
    	            	  			
    	            	  
    	            	  //2ND "FOR"
    	            	  		for(int j = 0; j < English.length; j++){
    	            	  				//change english to leet where necessary and add it to result string
    	            	  		
    	            	  			if(s.equals(English[j])){
    	            	  			  System.out.print(l33t[j]);
    	            	  			}
    	            	  			else{
    	            	  			  System.out.print(s);
    	            	  			}
    	            	  						
    	            	  					
    	            	  					
    	            	  			//		inputArray = input.toCharArray();//define array of chars (s) as the input in char Array form
    	            	 		// String inputArray2 = inputArray.toString();
    	       			
    	            	 		 
    	            	 		
    	            	 			
    	            	 	
    	            	    }
    	            	}
    	         
    	            		}//End of main 
    			
    	            	 
    		
    			
    					} //End of translator

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

    Default

    Here is the easiest way for me to explain the flaw in your program.
    Java Code:
    for(int j = 0; j < english.length; j++){
      if(s.equals(english[j]){
        System.out.print(l33t[j]);
      }
      else{
        System.out.print(s);
      }
    }
    The flaw is there, fixing this should get you the correct output.
    What is this piece of code doing? If the items match what does it do? Should it keep looping and comparing s to english[j]? If it doesn't match, should it print the regular item and keep looping?

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

    Default

    thanks sunde887. i get where the flaw is.... it compares it litaraly compared each input char with each character in English[] and for each one it print either the original. how do i make it only print one version?

  17. #37
    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 goal is to use something to break out or a loop, know anything that does that? If you do, use it. Try changing it and then exiting the inner loop, you only need to test if its equal, you dont need the else statement.

    Finally, there is still a flaw in this, however, I believe you should think about it if you still dont get the correct answer and consider where you are going wrong

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

    Default

    thanks sunde887! i completed the program and it works perfectly!

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

    Default

    Your welcome.

    This is the approach I took when I started helping you
    Java Code:
    public class Translator{
      public static String convert(String str){
        String result = "";
        String[] english = { a, i, e, l };
        String[] l33t = { 4, |, 3, 1 };
        for(int i = 0; i < english.length; i++){
          if(str.equalsIgnoreCase(english[i]){
            result = str;
            break;
          }
        }
        return result;
      }
      public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        System.out.println("Please enter a sentence to translate:");
        String sentence = scan.nextLine();
        String result = "";
        for(int i = 0; i < sentence.length(); i++){
          result += convert(sentence.subString(i, i + 1));
        }
        System.out.println(result);
      }
    }

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 3
    Last Post: 10-12-2010, 04:21 PM
  2. Replies: 1
    Last Post: 01-21-2010, 09:20 AM
  3. getDisplayLanguage returns "en" not "English"
    By DD70 in forum New To Java
    Replies: 6
    Last Post: 08-12-2009, 11:22 PM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07: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
  •