Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 java.lang.ArrayIndexOutOfBoundsException error
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
mapleleafs




PostPosted: Sat Jul 24, 2004 2:03 pm   Post subject: java.lang.ArrayIndexOutOfBoundsException error

this is my program(actually from a book)
code:
class truncator{
       
        static int maxlength=10;
       
        public static void main (String args[]) {
                String x=args[0];
                if(x.length()>maxlength)
                        {x=x.substring(0,maxlength);}
                System.out.println(x);
        }
}


and i get this error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at truncator.main(truncator.java:6)


i'm a newbie at java so help is much appreciated
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sat Jul 24, 2004 6:12 pm   Post subject: (No subject)

That means that the array isn't long enough for the index you're trying to access.

in Java (as in many other programing languages) zero is the first index of an array. If the array is empty, though, then attempting to access the zero-th element will result in an ArrayIndexOutOfBoundsException.

Try:

code:
public class Truncator {
   static int maxLength = 10;
 
   public static void main(String[] args) {
      try {
         String x = args[0];
         if (x.length() > maxLength)
            x = x.substring(0, maxLength);
         System.out.println(x);
      } catch(ArrayIndexOutOfBoundsException, e) {
         System.err.println("Sorry, you must provide a string to truncate.");
      }
   }
}
rizzix




PostPosted: Sun Jul 25, 2004 1:24 am   Post subject: (No subject)

actually ur better off not using a try block for this kinda error handling.. cuz is best u handle it in ur code.

use a if statement and check the length of the args array. if it is greater than zero.. u can do whatever u what else u do something else.

now if there is no something else whatsoever. then i suggest using the try-block.
guruguru




PostPosted: Sun Jul 25, 2004 12:34 pm   Post subject: (No subject)

code:

class truncator{
   
   static int maxlength=10;
   String x;
   
   public static void main (String args[]) {
      if (args[0] = null)
          x = "No string.";
      else
          x=args[0];
      if(x.length()>maxlength)
         {x=x.substring(0,maxlength);}
      System.out.println(x);
   }
}


I bellieve that should work Confused .
wtd




PostPosted: Sun Jul 25, 2004 2:52 pm   Post subject: (No subject)

Except that there's no reason x shouldn't be local to the main method.
guruguru




PostPosted: Sun Jul 25, 2004 8:02 pm   Post subject: (No subject)

Wow... umm ok...

code:
class truncator{
   
   static int maxlength=10;
   
   public static void main (String args[]) {
      String x;
      if (args[0] = null)
          x = "No string.";
      else
          x=args[0];
      if(x.length()>maxlength)
         {x=x.substring(0,maxlength);}
      System.out.println(x);
   }
}
Confused
wtd




PostPosted: Mon Jul 26, 2004 2:47 am   Post subject: (No subject)

Perhape even better...

code:
class truncator{
   
   static int maxlength=10;
   
   public static void main (String args[]) {
      String x;
      if (args.length == 0)
          x = "No string.";
      else
          x=args[0];
      if(x.length()>maxlength)
         {x=x.substring(0,maxlength);}
      System.out.println(x);
   }
}
wtd




PostPosted: Tue Jul 27, 2004 4:58 pm   Post subject: (No subject)

As a matter of convention, all class names in Java should begin with a capital letter.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: