#1
  1. PHP & Java Error Master
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    My Computer
    Posts
    1,219
    Rep Power
    0

    More Array Problems


    i'm defining an array
    Code:
    <%! public final static int[] DAYSINMONTH = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; %>
    And later, using it like this
    Code:
    int endCell = DAYSINMONTH[month] + startCell - 1;
    Now, month is defined in the url argument. (?month=7)
    What I tried to doing is using try/catch to catch the ArrayIndexOutOfBoundsException that pops up if the user enters a character in place of the month. Well, it doesn't catch it. So, I put an if before month's definition so that if it's an invalid value it won't use what the user submitted. Well, it doesn't work

    I'm getting rather annoyed at this as it seemed to be working yesterday.
    PHP Code:
    if (request.getParameter("month") == null)
    {
        
    // It's the current month
        
    month thisCal.get(Calendar.MONTH);
    }
    else
    {
        try
        {
            
    // Not the current month, get it from the uri
            
    if(Integer.parseInt(request.getParameter("month")) == || Integer.parseInt(request.getParameter("month")) == || Integer.parseInt(request.getParameter("month")) == || Integer.parseInt(request.getParameter("month")) == || Integer.parseInt(request.getParameter("month")) == || Integer.parseInt(request.getParameter("month")) == || Integer.parseInt(request.getParameter("month")) == || Integer.parseInt(request.getParameter("month")) == || Integer.parseInt(request.getParameter("month")) == || Integer.parseInt(request.getParameter("month")) == || Integer.parseInt(request.getParameter("month")) == 10 || Integer.parseInt(request.getParameter("month")) == 11)
            {
                
    month Integer.parseInt(request.getParameter("month"));
            }
            else
            {
                
    month thisCal.get(Calendar.MONTH);
                %>
                <
    center><span class="error">Invalid monthEnter a value between 0 (January) & 11 (December)
                </
    span><br/><br/></center>
                <%
            }
        }
        catch (
    Exception exc
        {
        %>
            <
    span class="error">An unexpected error has occured.<br/>
            <
    a href="javascript:history.back()">Click here to go back</a></span>
        <%
        }

    .
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Posts
    45
    Rep Power
    14
    Just try to parse whatever is passed and if its not a number it will throw an exception and you can display your message.

    if (request.getParameter("month") == null)
    {
    // It's the current month
    month = thisCal.get(Calendar.MONTH);
    }
    else
    {
    try
    {
    // Not the current month, get it from the uri
    month = Integer.parseInt(request.getParameter("month"));
    }
    catch (Exception exc)
    {
    month = thisCal.get(Calendar.MONTH);
    %>
    <center><span class="error">Invalid month. Enter a value between 0 (January) & 11 (December)
    </span><br/><br/></center>
    <%
    }
    }
  4. #3
  5. PHP & Java Error Master
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    My Computer
    Posts
    1,219
    Rep Power
    0
    What I tried to doing is using try/catch to catch the ArrayIndexOutOfBoundsException that pops up if the user enters a character in place of the month. Well, it doesn't catch it.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Posts
    45
    Rep Power
    14
    Sorry, I guess im not following your partial code shown. I don't see where you would be throwing an index out of bounds from the code you have posted.

    Question? If this is a web application why not use javascript to prevent them from entering a character in the form. I assume this is a form?
  8. #5
  9. PHP & Java Error Master
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    My Computer
    Posts
    1,219
    Rep Power
    0
    No, it's data from the url. I have no idea how javascript could help in this instance.

    And the try/catch is pretty clear.
    the integer array DAYSINMONTH is what throws the exception when month isn't an integer.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Posts
    45
    Rep Power
    14
    the url has to come from somewhere (user input) right? and your not calling "DAYSINMONTH" in the try catch. you are trying to get a parameter from the request in the try/catch which is why I assumed you are using a form.

    Good luck with that!
  12. #7
  13. PHP & Java Error Master
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    My Computer
    Posts
    1,219
    Rep Power
    0
    Ya, i just realised i didn't have the try/catch at the array.
    So i put one there without anything in the catch, and the previous catches work just fine.
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    5
    Rep Power
    0
    why dont u just use a dropdown box in the form then nobody can input an unexpected character!

IMN logo majestic logo threadwatch logo seochat tools logo