Results 1 to 9 of 9
  1. #1
    tonie is offline Member
    Join Date
    Jun 2013
    Posts
    16
    Blog Entries
    1
    Rep Power
    0

    Default [HELP] java.lang.ArrayIndexOutOfBoundsException

    Hi, my file compiles just fine but it gave me this error when i executed it. I'm suspecting it has to do with my tokenizer but I can't solve it.

    Java Code:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
    	at NetworkGUI.createNetwork(NetworkGUI.java:24)
    	at NetworkGUI.<init>(NetworkGUI.java:73)
    	at NetworkGUI.main(NetworkGUI.java:306)
    Java Code:
    	public void createNetwork()
    	{
    		Scanner input;
    		String word;
    		String tokenArray[];
    		
    		try
    		{
    			input = new Scanner(new File("networks.txt"));
    			
    			while(input.hasNext())
    			{
    				word = input.nextLine();
    				tokenArray = word.split(";");
    				
    				if(tokenArray[0].equals("public"))
    				
    					networkList.add(new PublicNetwork(tokenArray[1], tokenArray[2], 
    					new Address (tokenArray[3],tokenArray[4],tokenArray[5],
    					Integer.parseInt(tokenArray[6])), tokenArray[7]));
    				else
    					networkList.add(new CommercialNetwork(tokenArray[1], tokenArray[2], 
    					new Address (tokenArray[3],tokenArray[4],tokenArray[5],
    					Integer.parseInt(tokenArray[6])), tokenArray[7], tokenArray[8]));
    			}
    		}

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    902
    Rep Power
    2

    Default Re: [HELP] java.lang.ArrayIndexOutOfBoundsException

    Apparently tokenArray[8] does not exist. System.out.println the word variable and to see why it doesn't have the 8th field. This is debugging 101....

  3. #3
    tonie is offline Member
    Join Date
    Jun 2013
    Posts
    16
    Blog Entries
    1
    Rep Power
    0

    Default Re: [HELP] java.lang.ArrayIndexOutOfBoundsException

    Quote Originally Posted by SurfMan View Post
    Apparently tokenArray[8] does not exist. System.out.println the word variable and to see why it doesn't have the 8th field. This is debugging 101....
    I thought it was something like that, but I don't understand because I have this in the text file and it has 8 strings and 1 int. But this is what I got in the text file.

    Commercial;MOB;Joe Mamma;30tg Street Center;New York;Chicago;52562;Britney Cot;General Electric
    I'm sorry, but I don't know how to do SOP variable to check. I would appreciate if you could show me how.

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    902
    Rep Power
    2

    Default Re: [HELP] java.lang.ArrayIndexOutOfBoundsException

    Quote Originally Posted by tonie View Post
    I thought it was something like that, but I don't understand because I have this in the text file and it has 8 strings and 1 int. But this is what I got in the text file.



    I'm sorry, but I don't know how to do SOP variable to check. I would appreciate if you could show me how.
    Java Code:
    while(input.hasNext()) {
        word = input.nextLine();
        System.out.println("The line is " + word);
        tokenArray = word.split(";");
        System.out.println("The array contains: " + Arrays.toString(tokenArray));
                 
        if(tokenArray[0].equals("public")) {
            ....

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    902
    Rep Power
    2

    Default Re: [HELP] java.lang.ArrayIndexOutOfBoundsException

    Quote Originally Posted by tonie
    Hi, I get this error when I used the code you provided above.

    java:399: error: cannot find symbol
    System.out.println("The array contains: " + Array.toString(tokenArray));
    ^
    symbol: variable Array
    location: class NetworkGUI
    1 error
    Quote Originally Posted by tonie
    BTW, this is the code I have along with what you gave me. let me know what you think the reason why it wont compile. Do I have to declare "Arrays"?

    Java Code:

    public void createNetwork()
    {
    Scanner input;
    String word;
    String tokenArray[];

    try
    {
    input = new Scanner(new File("networks.txt"));

    while(input.hasNext())
    {
    word = input.nextLine();
    System.out.print("The line is " + word);
    tokenArray = word.split(";");
    System.out.println("The array contains: " + Arrays.toString(tokenArray));

    if(tokenArray[0].equals("public"))

    networkList.add(new PublicNetwork(tokenArray[1], tokenArray[2],
    new Address (tokenArray[3],tokenArray[4],tokenArray[5],
    Integer.parseInt(tokenArray[6])), tokenArray[7]));
    else
    networkList.add(new CommercialNetwork(tokenArray[1], tokenArray[2],
    new Address (tokenArray[3],tokenArray[4],tokenArray[5],
    Integer.parseInt(tokenArray[6])), tokenArray[7], tokenArray[8]));
    }
    }

    catch(IOException ioe)
    {
    ioe.printStackTrace();
    }
    }
    Please don't send private messages with more questions. Keep these questions in this thread since it might help others as well.

    You have to import java.util.Arrays; for this to work.

  6. #6
    tonie is offline Member
    Join Date
    Jun 2013
    Posts
    16
    Blog Entries
    1
    Rep Power
    0

    Default Re: [HELP] java.lang.ArrayIndexOutOfBoundsException

    Quote Originally Posted by SurfMan View Post
    Please don't send private messages with more questions. Keep these questions in this thread since it might help others as well.

    You have to import java.util.Arrays; for this to work.
    Thank you! Ok, so I ran it and it matches the first line of what i have in the text file which is 8 slot. I still don't understand why it doesn't run?

    This is what I have in the txt file.

    Edit: maybe I'm too tired. It's 4 am here :-(

    Public;PBS;Paul Pillow;2100 Drive;North;Virginia;22654;Public Radio
    Commercial;CBS;Leslie Pillow;51 West North Street;New York;New York;10019;Philippe Dicky;Viacom
    Commercial;ABC;Ben Pillow;54 West North Street;New York;New York;10017;Ed Noble;Disney
    Commercial;NBC;Jeff Pillow;30 North Center;New York;New York;10112;Bad Cox;General Electric
    Last edited by tonie; 10-18-2013 at 11:02 AM.

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    902
    Rep Power
    2

    Default Re: [HELP] java.lang.ArrayIndexOutOfBoundsException

    Your check is for "public", while the textfile contains "Public". Use "Public" or use equalsIgnoreCase().

  8. #8
    tonie is offline Member
    Join Date
    Jun 2013
    Posts
    16
    Blog Entries
    1
    Rep Power
    0

    Default Re: [HELP] java.lang.ArrayIndexOutOfBoundsException

    Quote Originally Posted by SurfMan View Post
    Your check is for "public", while the textfile contains "Public". Use "Public" or use equalsIgnoreCase().
    I was in bed but got out because I checked this post on my phone. I'm so happy that you helped me solve this problem suftman. THANK YOU!

  9. #9
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    902
    Rep Power
    2

    Default Re: [HELP] java.lang.ArrayIndexOutOfBoundsException

    Quote Originally Posted by tonie View Post
    I was in bed but got out because I checked this post on my phone. I'm so happy that you helped me solve this problem suftman. THANK YOU!

    LOL, glad to help out. Now go to sleep, you probably need it :)

Similar Threads

  1. Replies: 3
    Last Post: 12-04-2011, 12:50 PM
  2. Java.lang.ArrayIndexOutOfBoundsException:0>=0
    By maitoti in forum Advanced Java
    Replies: 3
    Last Post: 04-04-2011, 05:03 AM
  3. java.lang.ArrayIndexOutOfBoundsException
    By niteshwar.bhardwaj in forum Java 2D
    Replies: 0
    Last Post: 02-13-2009, 07:29 AM
  4. java.lang.ArrayIndexOutOfBoundsException
    By mensa in forum Java 2D
    Replies: 7
    Last Post: 05-05-2008, 09:09 AM
  5. java.lang.ArrayIndexOutOfBoundsException
    By mew in forum New To Java
    Replies: 2
    Last Post: 12-02-2007, 09:40 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
  •