Results 1 to 11 of 11
  1. #1
    Join Date
    Dec 2014
    Posts
    11
    Rep Power
    0

    Default May someone please help me find out what is wrong here?

    Below is all the coding I have done trying to make my own Powerball input system. I am really struggling on how to fix this. I keep getting an ArrayOutOfBounds exception except, I don't know how to fix this to make it where it will check the numbers without screwing up with everything.
    May someone please be gracious enough to help me figure out how to fix this? I am trying to do this on my own but, I can't figure this out.





    Java Code:
    import java.util.Scanner;
    
    public class PowerballTest
    {
    	Scanner input = new Scanner(System.in);
    	
    	public static void main(String[] args)
    	{
    		Powerball.readInput();
    		Powerball.writeOutput();
    	}
    
    	
    }
    Java Code:
    import java.util.Scanner;
     
    public class Powerball {
     
        static Scanner input = new Scanner(System.in);
        static int pick[] = new int[5];
     
        public static void readInput() {
     
            String[] place = { "first", "second", "third", "fourth", "fifth" };
            int number = 0;
            boolean noDupes = true;
            int powerball = 0;
     
            for (int size = 0; size < 6; ++size) {
                while (noDupes == true) {
     
                    System.out
                            .println("Enter the "
                                    + place[size]
                                    + " Powerball number which should be greater than 0 and less than 60");
                    number = input.nextInt();
                    checkNumber(number, noDupes);
                    if (noDupes == true) {
                       
                       break;
                    } else {
                        System.out
                                .println("The number you have entered can not be a duplicate.");
                    }
                    
                    if (number > 0 && number <= 59) {
                        pick[size] = number;
                        break;
                    } else {
                        System.out
                                .println("Numbers cannot be less than 1 or greater than 59.");
                    }
     
                }
     
            }
            while (noDupes == true) {
                System.out
                        .println("Enter the Powerball number that is greater than 0 or lesser than 36");
                number = input.nextInt();
     
                if (number > 0 && number <= 36) {
                    powerball = number;
                    break;
                } else {
                    System.out
                            .println("Numbers cannot be less than 1 or greater than 35.");
                }
            }
        }
     
        public static void writeOutput() {
            System.out
                    .println("The numbers you have chosen for your Powerball Ticket are "
                            + pick[0]
                            + " "
                            + pick[1]
                            + " "
                            + pick[2]
                            + " "
                            + pick[3] + " " + pick[4]);
        }
     
        public static  boolean checkNumber(int number, boolean noDupes) {
            for (int k = 0; k < 6; k++)
            {
                if (pick[k] == number )
                {
                	noDupes = false;
                	break;
                }
                else
                {
                	noDupes = true;
                }
            }
             return noDupes;
        }
    }
    This is the problem i keep seeing when the test run happens :

    Enter the first Powerball number which should be greater than 0 and less than 60
    2
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
    at Powerball.checkNumber(Powerball.java:73)
    at Powerball.readInput(Powerball.java:23)
    at PowerballTest.main(PowerballTest.java:9)

    (Don't know if should have formatted that^)
    Last edited by MysterioUnknown; 12-05-2014 at 07:56 PM.

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    384
    Rep Power
    10

    Default Re: May someone please help me find out what is wrong here?

    An ArrayIndexOutOfBoundsException is thrown when an array has been accessed with an illegal index. That illegal index could be a negative number or one >= the size of the array. The pick array has a size of 5, which means that the highest index you can access in the pick array is 4. Take a look at your for loop logic.

  3. #3
    Join Date
    Dec 2014
    Posts
    11
    Rep Power
    0

    Default Re: May someone please help me find out what is wrong here?

    That's the problem!!!! I have fixed all of the arrays and nothing is working. I am using "Eclipse" and I can't figure out where the problem is. It is telling me where it is but, it I've tried to fix it. I am frustrated to tears right now. Also I fixed the code above to make it match what i have now. but it isnt running still. This is so infuriating!!! I had this to where the numbers were stored but, now it wont even get past the very first freaking one!
    Last edited by MysterioUnknown; 12-05-2014 at 07:57 PM.

  4. #4
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    384
    Rep Power
    10

    Default Re: May someone please help me find out what is wrong here?

    If you've made the adjustments to the code, what happens when you recompile the application and run it again?

  5. #5
    Join Date
    Dec 2014
    Posts
    11
    Rep Power
    0

    Default Re: May someone please help me find out what is wrong here?

    I receive the same error in the coding. It is saying the error is at (Powerball.java:73) in the lines of code. I honestly don't know what is happening right there. I can't figure it out for the life of me.

  6. #6
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    384
    Rep Power
    10

    Default Re: May someone please help me find out what is wrong here?

    The problem doesn't sound like it's anything major. Can you post the updated error, along with the code?

  7. #7
    Join Date
    Dec 2014
    Posts
    11
    Rep Power
    0

    Default Re: May someone please help me find out what is wrong here?

    Java Code:
    import java.util.Scanner;
     
    public class Powerball {
     
        static Scanner input = new Scanner(System.in);
        static int pick[] = new int[5];
     
        public static void readInput() {
     
            String[] place = { "first", "second", "third", "fourth", "fifth" };
            int number = 0;
            boolean noDupes = true;
            int powerball = 0;
     
            for (int size = 0; size < 6; ++size) {
                while (noDupes == true) {
     
                    System.out
                            .println("Enter the "
                                    + place[size]
                                    + " Powerball number which should be greater than 0 and less than 60");
                    number = input.nextInt();
                    checkNumber(number, noDupes);
                    if (noDupes == true) {
                       
                     
                    } else {
                        System.out
                                .println("The number you have entered can not be a duplicate.");
                    }
                    
                    if (number > 0 && number <= 59) {
                        pick[size] = number;
                        break;
                    } else {
                        System.out
                                .println("Numbers cannot be less than 1 or greater than 59.");
                    }
     
                }
     
            }
            while (noDupes == true) {
                System.out
                        .println("Enter the Powerball number that is greater than 0 or lesser than 36");
                number = input.nextInt();
     
                if (number > 0 && number <= 35) {
                    powerball = number;
                    break;
                } else {
                    System.out
                            .println("Numbers cannot be less than 1 or greater than 35.");
                }
            }
        }
     
        public static void writeOutput() {
            System.out
                    .println("The numbers you have chosen for your Powerball Ticket are "
                            + pick[0]
                            + " "
                            + pick[1]
                            + " "
                            + pick[2]
                            + " "
                            + pick[3] + " " + pick[4]);
        }
     
        public static  boolean checkNumber(int number, boolean noDupes) {
            for (int k = 0; k < 6; k++)
            {
                if (number == pick[k] )
                {
                	noDupes = false;
                	break;
                }
                else
                {
                	noDupes = true;
                	continue;
                }
            }
             return noDupes;
        }
    }

    Enter the first Powerball number which should be greater than 0 and less than 60
    2
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
    at Powerball.checkNumber(Powerball.java:73)
    at Powerball.readInput(Powerball.java:23)
    at PowerballTest.main(PowerballTest.java:9)

  8. #8
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    384
    Rep Power
    10

    Default Re: May someone please help me find out what is wrong here?

    Your for loop still has k<6
    Remember, arrays are 0 based, so when you want the 1st element in the list, you use index 0, 2nd element in the list is index 1, etc.

  9. #9
    Join Date
    Dec 2014
    Posts
    11
    Rep Power
    0

    Default Re: May someone please help me find out what is wrong here?

    but the array is supposed to be 5. I dont get this. something isnt connecting

  10. #10
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    384
    Rep Power
    10

    Default Re: May someone please help me find out what is wrong here?

    Right. The array has a size of 5, but because arrays are 0 based, the 5th element is indicated with an index of 4.
    So, as your for loop is running, here is what it looks like:

    //1st loop, k=0, so pick[k] is a reference to the 1st element in the array
    //2nd loop,k is incremented, so now k=1, so pick[k] is a reference to the 2nd element in the array
    //3rd loop,k is incremented, so now k=2, so pick[k] is a reference to the 3rd element in the array
    //4th loop,k is incremented, so now k=3, so pick[k] is a reference to the 4th element in the array
    //5th loop,k is incremented, so now k=4, so pick[k] is a reference to the 5th element in the array
    //6th loop,k is incremented, so now k=5, so pick[k] is a reference to the 6th element in the array

    Do you see a problem?

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: May someone please help me find out what is wrong here?

    The array is of size 5. Look at line 15 and read sehudson's last post.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Cannot find or load or find main class
    By cornelombaard in forum New To Java
    Replies: 3
    Last Post: 11-19-2014, 03:04 PM
  2. Replies: 7
    Last Post: 04-30-2014, 12:43 PM
  3. Replies: 4
    Last Post: 06-11-2013, 01:37 AM
  4. I can't find anything wrong with this but somehow it's wrong.
    By Biscuit Tickler in forum New To Java
    Replies: 2
    Last Post: 09-12-2012, 09:28 PM
  5. I don't find where I did wrong ...
    By allexif in forum New To Java
    Replies: 5
    Last Post: 04-23-2012, 03:57 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
  •