Results 1 to 4 of 4
  1. #1
    Sean_J is offline Member
    Join Date
    Feb 2010
    Posts
    16
    Rep Power
    0

    Default Index out of bounds Numeric analyzer.

    I get my program to compile correctly but when i run it it gives me an out of bounds error. Anyone know whats going on? And maybe a solution:
    Java Code:
    class AnalyzeDigits3 {
    public static void main(String[] args) {
    int number = Integer.parseInt(args[0]);
    String[] data = analyzeDigits(number);
    System.out.println(data[0]);
    System.out.println(data[1]);
    }
    
    public static String[] analyzeDigits(int number) {
    String n = Integer.toString( Math.abs(number) );
    StringBuffer evenDigits = new StringBuffer("even digits:");
    StringBuffer oddDigits = new StringBuffer("odd digits:");
    for (int index = 0; index < n.length(); index++) {
    char digit = n.charAt(index);
    switch (digit) {
    case '0': case '2': case '4': case '6': case '8':
    evenDigits.append(
    ( lastCharInBufferIsDigit(evenDigits) ? ", " : " ") + digit
    );
    break;
    
    case '1': case '3': case '5': case '7': case '9':
    oddDigits.append(
    ( lastCharInBufferIsDigit(oddDigits) ? ", " : " ") + digit
    );
    break;
    } //end switch selection
    } // end for loop
    return new String[]{evenDigits.toString(), oddDigits.toString()};
    } // end analyzeDigits method
    
    private static boolean lastCharInBufferIsDigit(StringBuffer buffer) {
    return Character.isDigit( buffer.charAt( (buffer.length() - 1) ) );
    }
    }// end AnalyzeDigits3 class
    Welcome to DrJava. Working directory is C:\Users\SeanJJ
    > java AnalyzeDigits3
    java.lang.ArrayIndexOutOfBoundsException: 0
    at AnalyzeDigits3.main(AnalyzeDigits3.java:3)

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    > java AnalyzeDigits3 12

    You are not providing any arguments.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,431
    Blog Entries
    7
    Rep Power
    20

    Default

    Did you pass an argument to your class? Your main( ... ) method needs one.

    kind regards,

    Jos

  4. #4
    Sean_J is offline Member
    Join Date
    Feb 2010
    Posts
    16
    Rep Power
    0

Similar Threads

  1. [SOLVED] Array index out of bounds exception
    By sruthi_2009 in forum New To Java
    Replies: 6
    Last Post: 03-23-2014, 10:16 AM
  2. array Index out of Bounds exception== 0
    By Allgorythm in forum New To Java
    Replies: 6
    Last Post: 02-11-2010, 04:02 PM
  3. Array Index Out of Bounds Exception
    By kool001 in forum New To Java
    Replies: 1
    Last Post: 12-03-2009, 07:42 AM
  4. New to Java, Arrays- index out of bounds
    By Connorhj in forum New To Java
    Replies: 8
    Last Post: 12-02-2009, 09:22 PM
  5. Array Index out of bounds
    By leapinlizard in forum New To Java
    Replies: 5
    Last Post: 04-29-2009, 05:11 AM

Posting Permissions

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