Results 1 to 7 of 7
  1. #1
    flaskvacuum is offline Member
    Join Date
    Jul 2009
    Posts
    2
    Rep Power
    0

    Default ArrayIndexOutOfBoundsException

    Java Code:
    private static void displayMale()
        {
            //Scanner myScanner = new Scanner(System.in);
            int outProgram = 0;
            String maleLine;
            Scanner myScanner = null;
            String[] maleData = new String[100];
           
            //ArrayList<Male> maleList;
            //maleList = new ArrayList<Male>();
           
            //ArrayList<String> test = new ArrayList<String>();
       
           
            try
            {
                //fileNotFoundException may occur
                myScanner = new Scanner(new File("nameList.txt"));
           
                while(myScanner.hasNextLine())
                {
                    maleLine = myScanner.nextLine();// io exception may occur
                    //String[] maleData = maleLine.split(":");
                    maleData = maleLine.split(":");
                   
                   
                //    int x = Integer.parseInt(maleInfo[1]);
                       
                //    System.out.println("Names of Male SuperStars");
                //    System.out.println("==========================");
       
                      int i = 0;
                     
                     
                      ArrayList<Male> maleList;
                    maleList = new ArrayList<Male>();
                     
                    maleList.add(new Male(maleData[i], maleData[i+1]));
                   
                   
                    for(Male maleLoop : maleList)
                    {
                   
                        System.out.println(maleLoop.getName());
                        System.out.println(maleLoop.getScore());
                   
                    }
                   
                    i++;
    }
    
    
    I kept on having ArrayIndexOutOfBound on maleData[].
    But i have declare the size that is big enough not to get out of bound.
    
    Basically it is just reading a data from a file with this format:
    
    john:0
    dan:0
    
    Thus i use the split to store it in a sting array and use [0] and [1] to store it into my class object with [0] = name and [1] as score. I manage to get what i wanted but the exception being thrown is not very helpful.
    
    Below is the result i got:
    
    John
    0
    Jack
    0
    Ben
    0
    Julian
    0
    Daniel
    0
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at VotingSystem.displayMale(VotingSystem.java:186)
    at VotingSystem.displayMenu(VotingSystem.java:56)
    at VotingSystem.main(VotingSystem.java:20)

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    765
    Rep Power
    6

    Default

    Hi,
    If u are unable to fix the array size and if u get the "ArrayIndexOutOfBounds" exception,then store it in ArrayList and using toArray() method and you convert into Array.

    -Regards
    Ramya
    Ramya:cool:

  3. #3
    flaskvacuum is offline Member
    Join Date
    Jul 2009
    Posts
    2
    Rep Power
    0

    Default

    Java Code:
    private static void displayMale()
        {
            //Scanner myScanner = new Scanner(System.in);
            int outProgram = 0;
            String maleLine;
            Scanner myScanner = null;
            String[] maleData = new String[100];
           
            //ArrayList<Male> maleList;
            //maleList = new ArrayList<Male>();
           
            ArrayList<String> test = new ArrayList<String>();
       
           
            try
            {
                //fileNotFoundException may occur
                myScanner = new Scanner(new File("nameList.txt"));
           
                while(myScanner.hasNextLine())
                {
                    maleLine = myScanner.nextLine();// io exception may occur
                    //String[] maleData = maleLine.split(":");
                    //maleData = maleLine.split(":");
    
                      test.add(maleLine.split(":");
                   
                   
                //    int x = Integer.parseInt(maleInfo[1]);
                       
                //    System.out.println("Names of Male SuperStars");
                //    System.out.println("==========================");
       
                      int i = 0;
                     
                     
                      ArrayList<Male> maleList;
                    maleList = new ArrayList<Male>();
                     
                    maleList.add(new Male(maleData[i], maleData[i+1]));
                   
                   
                    for(Male maleLoop : maleList)
                    {
                   
                        System.out.println(maleLoop.getName());
                        System.out.println(maleLoop.getScore());
                   
                    }
                   
                    i++;
    }

    error thrown at test.add(maleLine.split(":");

    ArrayList doesn't allow me to do the above.

    How can i get [0], [1] in Arraylist...?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Your code doesn't compile, so it would really help if you posted something that actually compiles, otherwise we don't know whether what we're looking at is actually the problem or not.

  5. #5
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    I don't see anything wrong w/ the code, so most likely you have alot of newlines in your nameList.txt to wards the end of file. like so:

    John:0\n
    Jack:0\n
    Ben:0\n
    Julian:0\n
    Daniel:0\n
    \n
    \n
    \n
    \n
    \n
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Quote Originally Posted by angryboy View Post
    I don't see anything wrong w/ the code
    And this is the problem with not actually being given code that we can copy and paste into our systems to look at...this doesn't compile. There are brackets missing, which implies we are missing bits of the logic.

    However, this:

    Java Code:
    test.add(maleLine.split(":");
    is wrong, since test is an ArrayList<String> and not an ArrayList<String[]>. Which is why there's an error...though it's a compilation error and therefore not "thrown".

    So, could we have a chunk of code that, at least, the brackets match...and if there's an error it would be useful to have the error cut and pasted here as well. Also, when you get an exception when you run your code could you point out where that exception was thrown, since we don't have all your code and therefore do not know which line is line 186.

  7. #7
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    Actually I was referring to the code in the OP. What's most likely happening here is that there are newlines at the end of the data file. So calling hasNextLine returns true, because \n != EOF. Then calling nextLine returns a string of "\n" and calling split returns a single array element: "\n". The error come when you tried to pass the second array element, which is not there. So you get an array out of bounds exception.

    You could use this instead:
    Java Code:
    while(scanner.hasNext(".+:.+")){
      String[] s = scanner.next().split(":");
      ...
    }
    Last edited by angryboy; 07-14-2009 at 06:30 PM.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

Similar Threads

  1. [SOLVED] ArrayIndexOutOfBoundsException
    By thelinuxguy in forum Advanced Java
    Replies: 1
    Last Post: 05-07-2009, 10:34 PM
  2. java.lang.ArrayIndexOutOfBoundsException
    By niteshwar.bhardwaj in forum Java 2D
    Replies: 0
    Last Post: 02-13-2009, 07:29 AM
  3. ArrayIndexOutofBoundsException help
    By filly444 in forum New To Java
    Replies: 9
    Last Post: 09-03-2008, 05:16 PM
  4. java.lang.ArrayIndexOutOfBoundsException
    By mensa in forum Java 2D
    Replies: 7
    Last Post: 05-05-2008, 09:09 AM
  5. ArrayIndexOutOfBoundsException
    By daredavil82 in forum New To Java
    Replies: 2
    Last Post: 12-14-2007, 09:29 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
  •