Arrays and outputs

I'm new to programming with java, so please be so kind as to my silly mistakes. My problem is that my code is not producing the desired result, which is to display all even array values ​​underneath each other and then all uneven array values ​​underneath 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 conclusion (using the NetBeans IDE which shows the errors I have, which I have included because I don't quite understand what that 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 several questions: why is the number 8 in my array not being printed with 10 and 20 and 6 is being printed separately as 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 that?

Any help would be appreciated! Thanks in advance:)

+3


source to share


3 answers


Must be <not <= for a loop.

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]);
        }

      

Place this code in your main method and execute. Hope this helps you in solving your problem.

+3


source


If I understand your question - and what do you want. The simplest 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


source


To answer your question: "why doesn't the number 8 in my array print from 10 and 20, but 6 is printed separately like 8?"

This is because of your 2-loop system (for + do for now). For example, at the time you print "20" with i = 1, you do i ++, giving you i = 2 and arr [i] = 7. At this point, you have a test while arr [ i]% 2 == 0 and of course 7% 2! = 0, so you exit the do while loop, go back to for, DO DIFFERENT i ++, giving you i = 3, and then go to yours if again : in this example, you 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 very difficult to explain the algorithm without a whiteboard;)

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

0


source







All Articles