3

I'm new to programming with java, so please be lenient with my silly errors. My problem is that my code isn't giving the output that I desire, which is to display all the even values of an array underneath each other and then all the uneven values of the array under each other. This is my code:

//main class
public class Even_number_array
{

  public static void main(String[] args)
  {

    array_class obj = new array_class();
    obj.set_numbers();
  }
}

//another class
public class array_class
{

  private int arr[] =
  {
    10, 20, 7, 8, 3, 6, 11, 9, 7, 45
  };

  public void set_numbers()
  {

    for (int i = 0; i <= arr.length; i++)
    {

      if (arr[i] % 2 == 0)
      {
        System.out.println("These even numbers were found in the array:");
        do
        {
          System.out.println(arr[i]);
          i++;
        }
        while (arr[i] % 2 == 0);
      }
      else if (arr[i] % 2 != 0)
      {
        System.out.println("uneven numbers found in array:");
        do
        {
          System.out.println(arr[i]);
          i++;
        }
        while (arr[i] % 2 != 0);
      }

    }

  }
}

And this is my output (using Netbeans IDE which shows the errors I have, which I have included because I don't quite understand what it means.):

These even numbers were found in the array:

10

20

These even numbers were found in the array:

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

8

These even numbers were found in the array:

6

uneven numbers found in array:

9

7

45

at even_number_array.array_class.set_numbers(array_class.java:35)

at even_number_array.Even_number_array.main(Even_number_array.java:12)

Java Result: 1

I have a few questions, why is the number 8 in my array not printing with 10 and 20, and 6 printing separately like 8? And also, is there a way that I can get user input using the Scanner class for the array and how would I go about doing this?

Any help would be welcomed! Thanks in advance :)

3 Answers 3

3

For loop should be < not <=.

System.out.println("Even Numbers");
for (int i=0; i < arr.length;i++){
   if((arr[i] % 2) == 0)
   {
      System.out.println(arr[i]);
   }
}
System.out.println("Odd Numbers");
for (int i=0; i < arr.length;i++){
   if((arr[i] % 2) != 0)
   {
      System.out.println(arr[i]);
   }
}

ArrayList<Integer> array = new ArrayList<Integer>();
        int[] intArray;
        Scanner scanner = new Scanner(System.in);
        int a = 0;
        while(a != -1)
        {
            System.out.println("Please enter an integer -1 to quit: ");
            a = scanner.nextInt();
            if(a != -1)
            {
                array.add(a);
            }
        }
        intArray = new int[array.size()];
        for(int i = 0; i < array.size(); i++)
        {
            intArray[i] = array.get(i);
        }
        for(int b = 0; b < intArray.length; b++)
        {
            System.out.println("Integer" + b + ": " + intArray[b]);
        }

Put this code in your main method and execute. I hope this helps you with your problem.

8
  • 1
    This is actually what you want. Elliott was on the right track.
    – brso05
    Commented Sep 11, 2014 at 14:31
  • Thanks so much! This definitely helped :) Commented Sep 11, 2014 at 14:39
  • No problem. You can use a scanner to populate the array. Look up the scanner class if you still need help I could probably post some code for you.
    – brso05
    Commented Sep 11, 2014 at 14:44
  • I know how to use the scanner class for normal variables, but using the scanner class with an array is really confusing, if you could help me with this problem it would be highly appreciated! Commented Sep 11, 2014 at 14:56
  • Why did you change mine from being the right answer?
    – brso05
    Commented Sep 11, 2014 at 15:17
3

If I understand your question - and what you want. The easiest solution (I see) is to loop twice,

// Print the even numbers first.
for (int i=0; i < arr.length;i++){
  if (arr[i] % 2 == 0) { // % 2 == 0 is even
    System.out.printf("arr[%d] = %d%n (even)", i, arr[i]);
  }
}
// Then the odd numbers.
for (int i=0; i < arr.length;i++){
  if (arr[i] % 2 != 0) { // % 2 != 0 is odd (or "uneven")
    System.out.printf("arr[%d] = %d%n (odd)", i, arr[i]);
  }
}
3
  • Why would you say that wouldn't it be better to loop once and store in 2 different list one is for odd and one for even ? Commented Sep 11, 2014 at 14:27
  • @Aeshang How would copying the values to two arrays be display all the even values of an array underneath each other and then all the uneven values of the array under each other? Commented Sep 11, 2014 at 14:28
  • Could you elaborate on what this would do differently? Commented Sep 11, 2014 at 14:30
0

To answer your question : "why is the number 8 in my array not printing with 10 and 20, and 6 printing separately like 8?"

It's because of your 2 loops system (for + do while). For example, at the time you print "20" with i = 1, then you do a i++ giving you i = 2 and arr[i] = 7. At this moment, you have a test in your while arr[i]%2==0 and of course 7%2 != 0 so you exit your do while loop, go back to the for, DO ANOTHER i++ giving you i = 3 and then go to your if again : in this example you have never printed the value arr[2] = 7 and you print again "These even numbers were found in the array:" with the value arr[3] = 8.

I hope the explanation is clear enough. It's quite tough to explain algorithm without a white board ;)

PS : +1 for the "For loop should be < not <=" in this case.

1
  • Thank you! you're right about the explanation part, but I kinda get what you're saying :) Thanks for the help @Julien Commented Sep 11, 2014 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.