Results 1 to 9 of 9
  1. #1
    Connorhj is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Question New to Java, Arrays- index out of bounds

    Hey there, I'm new to java and I was wondering if there was a way to ignore or throw the exception index out of bounds. As at the moment in my program I'm trying to do validation, so if the user enters a number not within the array values then an error message in System.out.println will display instead of the exception stopping the whole program.

    Any help is appreciated, thanks :)

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    You should not accept any input that is invalid. Check the input.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Connorhj View Post
    Hey there, I'm new to java and I was wondering if there was a way to ignore or throw the exception index out of bounds. As at the moment in my program I'm trying to do validation, so if the user enters a number not within the array values then an error message in System.out.println will display instead of the exception stopping the whole program.

    Any help is appreciated, thanks :)
    Of course you can catch such an exception:

    Java Code:
    try {
       // code that can throw an AIOOB exception
    }
    catch (ArrayIndexOutOutBoundsException aioobe) {
       // handle the exception here
    }
    but if your code anticipates on such exception, and knowing that exception catching can be quite costly (in terms of virtual macine instructions) and knowing that it can be handled easily like this:

    Java Code:
    int i= ...; // some user supplied value
    if (i < 0 || i >= array.length)
       // i was out of bounds
    else 
       // do something with array[i]
    I prefer the second approach.

    kind regards,

    Jos
    Last edited by JosAH; 11-30-2009 at 02:36 PM.

  4. #4
    Half_Duplex is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    I would go with the second procedure JosAH mentioned, it is the most efficient and easiest. Plus it should be something to get used to, you will need implement some type of validation for every instance of user input everywhere it occurs.

  5. #5
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    5

    Default

    Quote Originally Posted by sr2000 View Post
    Can someone help please

    i have an array listofcolumnobjects which has 10 objects in it. I need to loop around this array until i come to the object i want to match to. Then i need to take the count of (i) then hit the tab key that many times
    don't open a thread and start spamming around on other topics with your question, just wait

  6. #6
    Connorhj is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default

    Thanks for the help so far, I went ahead with using the first method and it works great. The only part im still puzzled on now is after once my pogram has found the error I want it to carry on, as at the moment the program pauses when it finds the error. So is there any way to use the ArrayIndexOutOfBoundsException to just carry on after?

    I'll post a small bit of my program that I mean:

    Java Code:
            try {
               
            System.out.println(date(28, 2, 2006));
            System.out.println(date(31, 10, 2007));
            System.out.println(date(50, 5, 2008));
            System.out.println(date(28, 2,2007));
            System.out.println(date(3, 6, 2006));   
            
             } catch (ArrayIndexOutOfBoundsException e) {
                                                 
             }
    So on my third print line where the first number is 50, thats the part where the array is out of bounds and then the program pauses there without displaying the last two lines. So after now that its found an error I just simply want it to display the rest of the lines.
    Last edited by Connorhj; 11-30-2009 at 10:39 PM.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    Then either try/catch round each call to date(), or have date() handle the error.
    Is this just test data?

  8. #8
    chirag.jain18 is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default

    When any exceptions occurs in try block, flow of the program goes to catch, ignoring rest of the code in try block, so the code, after that code which generates exception, will not be executed.
    so there are only (according to me) 2 possible solutions for your problem:

    1. Use second approach given by JosAh, so that exception does not occur at all.

    2. Put every line in separate try catch, as:

    try {

    System.out.println(date(28, 2, 2006));
    }
    catch (ArrayIndexOutOfBoundsException e) {

    }

    try {
    System.out.println(date(31, 10, 2007));
    }

    catch (ArrayIndexOutOfBoundsException e) {

    }
    But this solution is not good, so use the first one!

  9. #9
    Connorhj is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

Similar Threads

  1. [SOLVED] Array index out of bounds exception
    By sruthi_2009 in forum New To Java
    Replies: 6
    Last Post: 03-23-2014, 10:16 AM
  2. Array Index out of bounds
    By leapinlizard in forum New To Java
    Replies: 5
    Last Post: 04-29-2009, 05:11 AM
  3. Java ArrayList out of bounds exception
    By grahamb314 in forum New To Java
    Replies: 5
    Last Post: 11-22-2008, 07:21 PM
  4. [SOLVED] out of bounds exception help
    By soxfan714 in forum New To Java
    Replies: 21
    Last Post: 11-11-2008, 08:16 AM
  5. why is my array out of bounds?
    By Phobos0001 in forum New To Java
    Replies: 3
    Last Post: 03-24-2008, 01:20 AM

Posting Permissions

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