1 2 Previous Next 23 Replies Latest reply: Sep 12, 2008 6:25 PM by 807589 RSS

    Char to int?

    807589
      public class StringToNumber {
           public StringToNumber() {}
           public static void main(String[] args) {
                new StringToNumber().run(args[0]);
           }     
           void run(String s) {
                System.out.println("Converting [" + s + "] to int:" + atoi(s));
           }
           private int atoi(String s) {
                // TODO: You must implement this
                int num = s.length();
                char[] t = s.toCharArray();  // not sure what to put after this line to convert the array of char to int
                //System.out.println(t);
                return num;
           }
      }
      Any suggestions would be appreciated
        • 1. Re: Char to int?
          807589
          Do you have to write your own parsing code from scratch, or can you use convenient methods defined in the API?
          • 2. Re: Char to int?
            807589
            I have to use recursion, but I have no idea how to yet.
            • 3. Re: Char to int?
              807589
              SoMeWhErEs wrote:
              I have to use recursion, but I have no idea how to yet.
              I would play around with some examples first. Recursion is a beautiful thing, almost holy. I tremble when I write recursive algorithms.
              • 4. Re: Char to int?
                807589
                SoMeWhErEs wrote:
                Any suggestions would be appreciated
                Sure, ask an actual question.
                • 5. Re: Char to int?
                  807589
                  public class StringToNumber {
                       public StringToNumber() {}
                       public static void main(String[] args) {
                            new StringToNumber().run(args[0]);  // I get the error message on this line
                       }     
                       void run(String s) {
                            System.out.println("Converting [" + s + "] to int:" + atoi(s));
                       }
                       private int atoi(String s) {
                            // TODO: You must implement this
                            if(s.length() == 0) {return 0;}
                            int num = s.length();
                            char[] t = s.toCharArray();
                            int q = Character.getNumericValue(t[0]);
                            int w = q*10^(num-1);
                            for(int e = s.length()- 1; e>0; e--){
                                 t[e-1] = t[e];
                            }
                            String str = new String(t);
                            return w + atoi(str);
                       }
                  }
                  I think I just about have this done, but I get an ArrayIndexOutOfBounds Exception. I am not sure how to fix it. I have been stuck on it for a while now.
                  • 6. Re: Char to int?
                    807589
                    BigDaddyLoveHandles wrote:
                    I would play around with some examples first. Recursion is a beautiful thing, almost holy. I tremble when I write recursive algorithms.
                    Is that why when I pray at the alter of recursion, the gods answer me with this cryptic message?
                    Exception in thread "main" java.lang.StackOverflowError
                    • 7. Re: Char to int?
                      807589
                      SoMeWhErEs wrote:
                      I think I just about have this done, but I get an ArrayIndexOutOfBounds Exception. I am not sure how to fix it. I have been stuck on it for a while now.
                      So? I still don't see a question.
                      • 8. Re: Char to int?
                        807589
                        What am i doing wrong that causes the error to occur??
                        • 9. Re: Char to int?
                          796440
                          SoMeWhErEs wrote:
                          What am i doing wrong that causes the error to occur??
                          What exact error, and where, exactly?
                          • 10. Re: Char to int?
                            807589
                            public class StringToNumber {
                                 public StringToNumber() {}
                            
                                 public static void main(String[] args) {
                                      new StringToNumber().run(args[0]);  //*** The exception comes up for this line ***
                                 }     
                                 void run(String s) {
                                      System.out.println("Converting [" + s + "] to int:" + atoi(s));
                                 }
                                 private int atoi(String s) {
                                      // TODO: You must implement this
                                      if(s.length() == 0) {return 0;}
                                      int num = s.length();
                                      char[] t = s.toCharArray();
                                      int q = Character.getNumericValue(t[0]);
                                      int w = q*10^(num-1);
                                      for(int e = s.length()- 1; e>0; e--){
                                           t[e-1] = t[e];
                                      }
                                      String str = new String(t);
                                      return w + atoi(str);
                                 }
                            }
                            This is the exact error message that I get: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
                                 at StringToNumber.main(StringToNumber.java:5)
                            • 11. Re: Char to int?
                              807589
                              When you run your program you need to actually pass a value to the command line arguments. If you don't then the args array (the parameter of your main method) will have a length of zero and thus this line fails.
                              new StringToNumber().run(args[0]);
                              • 12. Re: Char to int?
                                796440
                                SoMeWhErEs wrote:
                                public class StringToNumber {
                                     public StringToNumber() {}
                                
                                     public static void main(String[] args) {
                                          new StringToNumber().run(args[0]);  //*** The exception comes up for this line ***
                                     }     
                                     void run(String s) {
                                          System.out.println("Converting [" + s + "] to int:" + atoi(s));
                                     }
                                     private int atoi(String s) {
                                          // TODO: You must implement this
                                          if(s.length() == 0) {return 0;}
                                          int num = s.length();
                                          char[] t = s.toCharArray();
                                          int q = Character.getNumericValue(t[0]);
                                          int w = q*10^(num-1);
                                          for(int e = s.length()- 1; e>0; e--){
                                               t[e-1] = t[e];
                                          }
                                          String str = new String(t);
                                          return w + atoi(str);
                                     }
                                }
                                This is the exact error message that I get: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
                                     at StringToNumber.main(StringToNumber.java:5)
                                Exactly like the error message say: At line 5 of StringToNumber.java, you're trying to access the first element (at index 0) of an array that doesn't have any elements.

                                Out or curiosity, where did you think args[0] would come from?
                                • 13. Re: Char to int?
                                  807589
                                  This is painful to watch:
                                      int[] iArray = new int[myString.length()];
                                      for(int i=0; i<myString.length(); i++) iArray[i] = (int) myString.charAt(i);
                                  char's can just be cast to type int for conversion.
                                  • 14. Re: Char to int?
                                    807589
                                    morgalr wrote:
                                    This is painful to watch:
                                    int[] iArray = new int[myString.length()];
                                    for(int i=0; i<myString.length(); i++) iArray[i] = (int) myString.charAt(i);
                                    char's can just be cast to type int for conversion.
                                    True, but that maps '0' to 48, for example.
                                    1 2 Previous Next