Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: returning an integer

  1. #1
    Junior Member Tubbly's Avatar
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default returning an integer

    Hey guys,

    I'm trying to create a program that allows the user to enter the size of an array and then enter the contents of each subscript. after doing that, the program will check which entry is an even number (after each number entry) and create a new array, the size depending on the even number amount. Once it does that, it checks again through the original array and for each array that is even, it places the number in the new array. However when I compile my program, for testing purposes I output the evenCount and it gives 10x whatever even numbers there are.. also I get an Array out of bounds exception which I don't think should happen after looking at my code over and over again. Could I get a second opinion on my code?

    Thanks

    import java.util.Scanner;
     
     
    public class AllEven {
     
        public static void main (String[]args)
        {
            int evenCount =0;
     
            Scanner kybd = new Scanner(System.in);
     
            System.out.print("Enter size of first array: ");
            int size1 = kybd.nextInt();
            int[] array = new int[size1];
            System.out.println();
     
            System.out.print("Enter contents of first array: ");
            read(array, evenCount);
     
            System.out.print("Number of even numbers in array: " + read());
     
            System.out.println(evenCount);
            int[] evenArray = new int [evenCount];
            System.out.println();
     
     
     
            getEven(array, evenArray);
     
            print(evenArray);
     
        }
     
        public static int read(int[] arr, int even)
        {
     
            Scanner kybd = new Scanner(System.in);
            for (int i = 0; i < arr.length; i++)
            {
                arr[i] = kybd.nextInt();
                double OddOrEven = arr[i] % 2;
                if (OddOrEven > 0)
                {
                    System.out.println();
                }
                else
                {
                    even++;
                }    
     
            }
            System.out.print("Number of even numbers in array: " + even);
            return even;
        }
     
        public static int[] getEven(int[] arr, int[]EveArray)
        {
           int a = 0;
            System.out.println("TESTING1");
            for (int x = 0; x < arr.length; x++)
            {
                System.out.println("TESTING2");
     
                //for (int a = 0; a < EveArray.length; a++)
     
                    System.out.println("TESTIN3");
                    double oddOrEven = arr[x] % 2;
     
                    if (oddOrEven > 0)
                    {
                         System.out.println("number not copied over, as its not even");
                    }
                    else
                    {
                        System.out.println("TESTING4");
     
                        System.out.println("TESTING5");
                        EveArray[a] = arr[x]; 
                        EveArray[a]++;
     
                    }
     
            }
     
            return EveArray;
     
        }
     
        public static void print(int[] EveArray)
        {
            for (int i = 0; i < EveArray.length; i++)
            {
                System.out.print(EveArray[i] + " ");
            }
            System.out.println();
        }
    }

    Results:

    helpmeee!.jpg


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: returning an integer

    get an Array out of bounds exception
    Please post the full text of the error message. It shows where the exception happened and the value of the index.

    Remember that array indexes range from 0 to the array length-1
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member Tubbly's Avatar
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: returning an integer

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

    at AllEven.getEven(AllEven.java:79)
    at AllEven.main(AllEven.java:29)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: returning an integer

    What array is being indexed at line 79? The error message says that the array is empty because it does not have a first element at index 0.

    Add a println to print out the .length of all arrays used on line 79.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member Tubbly's Avatar
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: returning an integer

    oh I see, turns out the array size isnt being set to the correct size (0) I believe it may be when im returning the even integer in the read method. how would I use this return value to set the array size in the main method?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: returning an integer

    how would I use this return value to set the array size
    Here's an example of how to set an array size using a value returned by a method:
      int size1 = kybd.nextInt();
      int[] array = new int[size1];
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member Tubbly's Avatar
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: returning an integer

    I had already done that in lines 19 and 20:

    read(array, evenCount);
    int[] evenArray = new int [evenCount];

    no luck

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: returning an integer

    The code you posted did NOT use a value returned by a method to define the size of the array.
    To get a value from the method, the code would be:
    evenCount = read(array, evenCount);

    What did this line print out:?
       System.out.println(evenCount);
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member Tubbly's Avatar
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: returning an integer

    okay, I did the first line and it ended up not doing anything, (it would ask me to enter my values then just do nothing after I pressed enter) so I made a new variable called test and did

    int testing = read(array,evenCount);
    int[] evenArray = new int [testing];

    it then outputs the correct amount of even numbers horray! however it does that then continues to do nothing :S no errors or anything

    in your second question it output 0 however because ive changed it to testing it now works lol

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: returning an integer

    continues to do nothing
    Do you mean that the program is executing an endless loop?
    Add some println statements to find out where it is looping and what the values of the variables are that would keep it from exiting the loop.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member Tubbly's Avatar
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: returning an integer

    it appeared that I had:

    testing = read(array, evenCount);

    int testing = read(array, evenCount);

    which was causing the problem, fixed that now

    program is now working as intended! thanks alot Norm!!

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: returning an integer

    Glad you got it working.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Big Integer help
    By ZekeQR in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 18th, 2012, 09:03 AM
  2. Integer program help
    By oriordc2 in forum Member Introductions
    Replies: 3
    Last Post: February 26th, 2012, 02:38 PM
  3. Help with ArrayList<Integer>
    By shanklove in forum Collections and Generics
    Replies: 2
    Last Post: September 6th, 2010, 09:15 AM
  4. Returning Null
    By Woody619 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: July 22nd, 2010, 12:53 PM
  5. returning a 2D array
    By straw in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2010, 04:30 AM