10 Replies Latest reply: Jun 9, 2009 2:01 AM by 843789 RSS

    retrieving int values as codes to output text...I'm having alot of trouble.

    843789
      public class Code
      {
        public static void main(String[] args)
        {
          String quick[] = {"", "The","quick","brown","fox","jumps","over","the","lazy", 
              "dog."};
          
          String code = "34829163";
          //int code1 = Integer.parseInt(code);
          
          for (int i=0; i<code.length(); i+=2)
          {      
            char decode = quick[code.charAt(i)].charAt(i+1);
            System.out.print(decode);
          } // suppose to print 1 letter for every 2 numbers in
        } // main method
      } // end of code class
      Okay basically the goal of this is to go through the sequence of numbers.
      The 1st number supposed to locate the word in the array.
      The 2nd number is supposed to locate the character in that word of the array.
      Meaning every 2 numbers = 1 letter decoded.
      This seemed like fairly easy logic, but I keep having out of bounds issues and what not and I know I'm missing something.
      Can anyone give me a hint or tell me what I'm doing wrong here?
        • 1. Re: retrieving int values as codes to output text...I'm having alot of trou
          794254
          The integer value of a the char '3' isn't the same as the integer value of 3. That hint should help you get started on the solution
          • 3. Re: retrieving int values as codes to output text...I'm having alot of trou
            794254
            And for after you've fixed that, maybe you should put in checks for invalid code also. If I ask for the 10th character from the word 'brown', your code will throw an exception again.

            Also, when you're getting an exception, please post the complete stacktrace here so that it's easier to see what's going on. Don't paraphrase your errors (or your code), copy-paste the exact text.
            • 5. Re: retrieving int values as codes to output text...I'm having alot of trou
              843789
              public class Code
              {
                public static void main(String[] args)
                {
                  String quick[] = {" ","the","quick","brown","fox","jumps","over","the","lazy", 
                      "dog."};
                  
                  String code = "40815410";
                  
                  final int ZERO = 48;
                  final int ONE = 49;
                  final int TWO = 50;
                  final int THREE = 51;
                  final int FOUR = 52;
                  final int FIVE = 53;
                  final int SIX = 54;
                  final int SEVEN = 55;
                  final int EIGHT = 56;
                  final int NINE = 57;
                  
                  for (int i=0; i<code.length(); i+=2)
                  {      
                    char charWord = code.charAt(i);
                    char charLetter = code.charAt(i+1);
                    
                    int w = 0;
                    int l = 0;
                    
                    if (charWord == ZERO)
                    {
                      w = 0;
                    }
                    
                    if (charWord == ONE)
                    {
                      w = 1;
                    }
                    
                    if (charWord == TWO)
                    {
                      w = 2;
                    }
                    
                    if (charWord == THREE)
                    {
                      w = 3;
                    }
                    
                    if (charWord ==FOUR)
                    {
                      w = 4;
                    }
                    
                    if (charWord == FIVE)
                    {
                      w = 5;
                    }
                    
                    if (charWord == SIX)
                    {
                      w = 6;
                    }
                    
                    if (charWord == SEVEN)
                    {
                      w = 7;
                    }
                    
                    if (charWord == EIGHT)
                    {
                      w = 8;
                    }
                    
                    if (charWord == NINE)
                    {
                      w = 9;
                    }
                    
                    if (charLetter == ZERO)
                    {
                      l = 0;
                    }
                    
                    if (charLetter == ONE)
                    {
                      l = 1;
                    }
                    
                    if (charLetter == TWO)
                    {
                      l = 2;
                    }
                    
                    if (charLetter == THREE)
                    {
                      l = 3;
                    }
                    
                    if (charLetter == FOUR)
                    {
                      l = 4;
                    }
                    
                    if (charLetter == FIVE)
                    {
                      l = 5;
                    }
                    
                    if (charLetter == SIX)
                    {
                      l = 6;
                    }
                    
                    if (charLetter == SEVEN)
                    {
                      l = 7;
                    }
                    
                    if (charLetter == EIGHT)
                    {
                      l = 8;
                    }
                    
                    if (charLetter == NINE)
                    {
                      l = 9;
                    }
                    
                    char decode = quick[w].charAt(l);
                    System.out.print(decode);
                  } // suppose to print 1 letter for every 2 numbers in
                } // main method
              } // end of code class
              ouput:
              ------------------------------------------------------------------------------------------------
              fast
              ------------------------------------------------------------------------------------------------

              Okay so I got this part of my program working, but I had to use a ton of code. I tried parsing, casting and changing the char in as many ways as I could think of to make an int, but this is the best I could think of. Is there a perhaps more effective way to do what I did?

              To clarify...I'm trying to figure out how make this work without having to use a bunch of if statements. Something tells me there has got to be a more efficient way.
              • 6. Re: retrieving int values as codes to output text...I'm having alot of trouble.
                843789
                I have another question. My new question is that I want to know if there a more efficient solution rather than the solution that I found.
                • 8. Re: retrieving int values as codes to output text...I'm having alot of trouble.
                  843789
                  public class Parse
                  {
                    public static void main(String[] args)
                    {
                      String quick[] = {" ","the","quick","brown","fox","jumps","over","the","lazy", 
                          "dog."};
                      
                      String code = "40815410";
                         
                      for (int i=0; i<code.length(); i+=2)
                      {      
                        int w = Integer.parseInt(code.charAt(i));
                        int l = Integer.parseInt(code.charAt(i+1));
                        
                        char decode = quick[w].charAt(l);
                        System.out.print(decode);
                      } // suppose to print 1 letter for every 2 numbers in
                    } // main method
                  } // end of code class
                  --------------------------------------------------------------------------------------------------------------

                  Exception in thread "main" java.lang.Error: Unresolved compilation problems:
                       The method parseInt(String) in the type Integer is not applicable for the arguments (char)
                       The method parseInt(String) in the type Integer is not applicable for the arguments (char)

                       at Parse.main(Parse.java:12)
                  --------------------------------------------------------------------------------------------------------------

                  Yea, I thought that would work too. I also tried converting the char to a string first, but I was unsuccessful at pulling that off. Thanks though.
                  • 9. Re: retrieving int values as codes to output text...I'm having alot of trou
                    794254
                    stuckne1 wrote:
                    To clarify...I'm trying to figure out how make this work without having to use a bunch of if statements. Something tells me there has got to be a more efficient way.
                    There is, it's a switch, it works with numerical values (chars can be used cast as numerical values), that's your first improvement.

                    However, once you realize that you can easily convert the character to the numerical it represents, you can do away with the switch/ if-else completely. A char maps to ASCII for the first 128 values. Notice that all the numbers in ASCII are sequential and 0 is represented by decimal 48. So if you subtract 48 from a char representing '0', what do you have? The number 0. And if you did this for a char holding '3', you'd get the number 3. That immediately pares down your code.

                    But keep in mind that this will work for non-numeric characters also! So you can easily subtract 48 from 'x' to get 72! But that's not valid for your usecase so you need to have something else that sanitizes your input and makes sure the letters represent numbers only.
                    • 10. Re: retrieving int values as codes to output text...I'm having alot of trou
                      843789
                      import java.util.Scanner;
                      
                      /**************************************************************
                      * Code.java
                      * Steven Tuckness
                      *
                      * 
                      * 
                      **************************************************************/
                      
                      public class Decode
                      {
                        public static void main(String[] args)
                        {
                          String quick[] = {" ","the","quick","brown","fox","jumps","over","the","lazy", 
                              "dog."};
                          
                          Scanner stdIn = new Scanner(System.in);
                          
                          System.out.println("Enter what you would like to decode.");
                          String code = stdIn.nextLine();
                          
                          for (int i=0; i<code.length(); i+=2)
                          {         
                            int w = code.charAt(i);
                            int l = code.charAt(i+1);
                            char decode = quick[w-48].charAt(l-48);
                            System.out.print(decode);
                          } // suppose to print 1 letter for every 2 numbers in
                        } // main method
                      } // end
                      --------------------------------------------------------------------------------------------------------------
                      Here is a start that works, thank you very much!