Results 1 to 2 of 2
  1. #1
    MBD
    MBD is offline Member
    Join Date
    Sep 2011
    Location
    Sale, Cheshire
    Posts
    10
    Rep Power
    0

    Default java.lang.ArrayIndexOutOfBoundsException error

    Hi, please can anyone explain why I get this error:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 atTokenScanner1.main(TokenScanner1.java:13

    Java Code:
    import java.util.Scanner;
    import java text.*;
    
    class TokenScanner1 {
    
    public static void main (String[] args) {
    
    	boolean b, b2;
    
    	int i;
    
    	String s, hits = " ";
    
    	Scanner s1 = new Scanner(args[0]);
    
    	while (b = s1.hasNext())  {
    
    	if (s1.hasNextInt())   {
    
    	  i = s1.nextInt(); hits += "i"; }
    
    	else if (s1.hasNextBoolean()) {
    	  
                 b2 = s1.nextBoolean(); hits += "b"; }
    	
                 else { s1.next(); hits += "o"; }  }
    	
                 System.out.println ("hits " + hits); } }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: java.lang.ArrayIndexOutOfBoundsException error

    You're not passing any command line arguments when you call your program and so the args array has size 0 -- has no members. Solution: always pass appropriate command line arguments when you run this program or any program that needs them. Also you should check that the user has done this in your main before trying to get the argument. You could check the length of the args array and if it is 0, warn the user that they're not running the program correctly, and then cleanly exit the program.

Similar Threads

  1. java.lang.ArrayIndexOutOfBoundsException: 1
    By Aparajita in forum New To Java
    Replies: 3
    Last Post: 08-20-2011, 11:26 PM
  2. Replies: 1
    Last Post: 11-20-2009, 07:58 PM
  3. java.lang.ArrayIndexOutOfBoundsException
    By mensa in forum Java 2D
    Replies: 7
    Last Post: 05-05-2008, 09:09 AM
  4. Replies: 1
    Last Post: 07-31-2007, 11:47 PM
  5. Replies: 1
    Last Post: 07-25-2007, 10:55 PM

Posting Permissions

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