Results 1 to 6 of 6
  1. #1
    awadjoseph is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Question java.lang.ArrayIndexOutOfBoundsException - Can't seem to stay in bounds?

    I've written this code for an assignment due tonight at 12:00. I feel like everything works EXCEPT the array, which is causing me lots of grief. I've tried everything, and after hours of debugging, I always end up back at the error:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
    at PercentOfSum.input2(PercentOfSum.java:35)
    at PercentOfSum.main(PercentOfSum.java:62)
    Here is my code:

    Java Code:
    //Author: ******
    //ID: *********
    //Date: November 27, 2012
    import java.util.Scanner;
    public class PercentOfSum
    {
    	
        public static void header ()
    	{
            System.out.println ("#########################################");        
            System.out.println ("Assignment #8\nAuthor: ********\nID: ***********\nDate: November 27, 2012");
            System.out.println ("This program solves project 1 from");
            System.out.println ("chapter 6 of Savitch book.");
            System.out.println ("#########################################");  
        }
        
        public static int input1 ()
        {
            Scanner kbd = new Scanner (System.in);
            System.out.println ("How many numbers will you enter?");
            int i = kbd.nextInt();
            
            return i;
        }
        
        public static int input2 (int i)
        {
            Scanner kbd = new Scanner (System.in);        
            System.out.println ("\nEnter " + i + " numbers,\none per line.");
            int n;        
            int [] x = new int [i];
            for (n = 0; n < i; n++)
            {
                System.out.println ("Enter number " + (n+1) + ":");
                x[n] = kbd.nextInt();
            }
            return x[n];
        }
    
        public static int output (int [] x, int i)
        {
            System.out.println ("You have filled the array with " + i + " numbers.\n\n");
            int sum = 0;        
            for (int p = 0; p < x.length; p++)
            {
                sum += x[p];
            }
            System.out.println ("The sum of the numbers = " + sum);
            System.out.println ("\nHere are the numbers and their\npercent contribution to the sum.\n");
            for (int j = 0; j < x.length; j++)
            {
                System.out.println (x[j] + " is " + ((x[j]/sum)*100) + "% of the sum.");
            }
            return sum;
        }
    
        public static void main (String [] a)
        {
            header();
            int i = input1 ();
            int [] x = new int [i];        
            x[i] = input2 (i);
            int sum = output (x,i);
            
    
        }
            
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: java.lang.ArrayIndexOutOfBoundsException - Can't seem to stay in bounds?

    What index are you trying to access? How many indexes does the array have? Use a debugger or add some print statements if you aren't sure.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: java.lang.ArrayIndexOutOfBoundsException - Can't seem to stay in bounds?

    Print out the value of n before line #37 (according to your listing) and you'll see why ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    awadjoseph is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default Re: java.lang.ArrayIndexOutOfBoundsException - Can't seem to stay in bounds?

    I changed the input2 method and it resolved my error on line 35, although I'm still getting an error at line 62.
    What I changed for the input2 method is
    Java Code:
    {
            Scanner kbd = new Scanner (System.in);        
            System.out.println ("\nEnter " + i + " numbers,\none per line.");
            int n;
            int [] x = new int [i];
            for (n = 0; n < i; n++)
            {
                System.out.println ("Enter number " + (n+1) + ":");
                x[n] = kbd.nextInt();
            }
            return n;
        }
    Now for line 62, I'm still getting the error
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at PercentOfSum.main(PercentOfSum.java:62), I don't know what to change now.

  5. #5
    awadjoseph is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default Re: java.lang.ArrayIndexOutOfBoundsException - Can't seem to stay in bounds?

    Okay I understand now that I'm only returning the value of x[n], whatever n is, and nothing before it. Thus this will only work if my value for n is 1 (I think.)
    Even considering this though, I'm not quite sure how I should fix this. I don't know how to make the method return more than just the value of x[n]. Also, my main method still isn't working correctly.

  6. #6
    awadjoseph is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default Re: java.lang.ArrayIndexOutOfBoundsException - Can't seem to stay in bounds?

    Nevermind, I solved it. Thanks

Similar Threads

  1. java.lang.ArrayIndexOutOfBoundsException
    By Grewe02 in forum New To Java
    Replies: 3
    Last Post: 09-18-2012, 08:38 PM
  2. java.lang.ArrayIndexOutOfBoundsException
    By rajasohaibmaroof in forum AWT / Swing
    Replies: 15
    Last Post: 09-29-2011, 07:46 PM
  3. java.lang.ArrayIndexOutOfBoundsException: 1
    By Aparajita in forum New To Java
    Replies: 3
    Last Post: 08-20-2011, 11:26 PM
  4. java.lang.ArrayIndexOutOfBoundsException
    By riccian in forum New To Java
    Replies: 0
    Last Post: 03-18-2008, 09:38 AM
  5. java.lang.ArrayIndexOutOfBoundsException
    By Marcus in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 05:15 AM

Tags for this Thread

Posting Permissions

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