
-----------------------------------
mapleleafs
Sat Jul 24, 2004 2:03 pm

java.lang.ArrayIndexOutOfBoundsException error
-----------------------------------
this is my program(actually from a book)
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

-----------------------------------
wtd
Sat Jul 24, 2004 6:12 pm


-----------------------------------
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:

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
Sun Jul 25, 2004 1:24 am


-----------------------------------
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
Sun Jul 25, 2004 12:34 pm


-----------------------------------

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  :? .

-----------------------------------
wtd
Sun Jul 25, 2004 2:52 pm


-----------------------------------
Except that there's no reason x shouldn't be local to the main method.

-----------------------------------
guruguru
Sun Jul 25, 2004 8:02 pm


-----------------------------------
Wow... umm ok...

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);
   }
}   :?

-----------------------------------
wtd
Mon Jul 26, 2004 2:47 am


-----------------------------------
Perhape even better...

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
Tue Jul 27, 2004 4:58 pm


-----------------------------------
As a matter of convention, all class names in Java should begin with a capital letter.
