Results 1 to 2 of 2
- 09-27-2011, 07:23 PM #1
Member
- Join Date
- Sep 2011
- Location
- Sale, Cheshire
- Posts
- 10
- Rep Power
- 0
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); } }
-
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
-
java.lang.ArrayIndexOutOfBoundsException: 1
By Aparajita in forum New To JavaReplies: 3Last Post: 08-21-2011, 12:26 AM -
Runtime error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
By shantimudigonda in forum New To JavaReplies: 1Last Post: 11-20-2009, 08:58 PM -
java.lang.ArrayIndexOutOfBoundsException
By mensa in forum Java 2DReplies: 7Last Post: 05-05-2008, 10:09 AM -
Error: java.lang.ArrayIndexOutOfBoundsException
By fernando in forum Java 2DReplies: 1Last Post: 08-01-2007, 12:47 AM -
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
By romina in forum New To JavaReplies: 1Last Post: 07-25-2007, 11:55 PM
Bookmarks