0

This code is to input n integer values and calculate the no. of even and odd values among those inputted values.

This java code shows ArrayIndexOutOfBoundsException when used do..while loop, however when used for loop it worked fine. Haven't changed any thing just rearranged the syntax so as to convert for loop into do while loop.

FOR LOOP:

import java.util.*;

public class EvenOddCount
{
    public static void main(String args[]) throws Exception
{
    System.out.print("Enter the no. of inputs to be taken : ");
    int evenCount=0, oddCount=0;
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int a[] = new int[n];
    System.out.println("Enter the inputs : ");
    for(int i=0;i<n;i++)
        a[i] = sc.nextInt();
    for(int i=0;i<a.length;i++)
    {
        if(a[i]%2==0)
            evenCount++;
        else 
            oddCount++;
    }
    System.out.println("\nThe number of even numbers in input numbers are : "+evenCount);
    System.out.println("The number of odd numbers in input numbers are : "+oddCount);
}
} 

The above code works fine and gives suitable output.

DO...WHILE LOOP:

import java.util.*;

public class EvenOddCount
{
public static void main(String args[]) throws Exception
{
    System.out.print("Enter the no. of inputs to be taken : ");
    int evenCount=0, oddCount=0, i=0;
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int a[] = new int[n];
    System.out.println("Enter the inputs : ");
    do
    {
        a[i] = sc.nextInt();
        i++;
    } while (i<n);
    do
    {
        if(a[i]%2==0)
            evenCount++;
        else 
            oddCount++;
        i++;
    } while (i<a.length);
    System.out.println("\nThe number of even numbers in input numbers are : "+evenCount);
    System.out.println("The number of odd numbers in input numbers are : "+oddCount);
}
} 

The above code has a runtime exception i.e.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at EvenOddCount.main(EvenOddCount.java:20)
meisner97
  • 9
  • 6
  • That's because do-while loops always execute at least once due to the condition being evaluated after. In your second do while loop, `int i` is still the same value as it was on the last iteration of the first do-while, and thus out range when doing `a[i]%2` – Andrew Li Sep 23 '18 at 17:54
  • @Li357 Thanks. I corrected it. – meisner97 Sep 23 '18 at 18:16

2 Answers2

5

In your original code, you had two separate i variables:

for(int i=0;i<n;i++)
...

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

In your do/while version, you've got one i variable. Once you've gone through the first loop, the value of i will be n - but you start on the second loop without resetting it to 0, so on the very first iteration it will be out of range.

You can fix that by adding:

i = 0;

just before the second do/while loop, but be aware that you'll still have a problem (even in the first loop) if n is 0, because you're not checking the condition until the end of the iteration. If you use:

while (i < n)

and

while (i < a.length)

instead, that will check the condition before the first iteration, so it will execute 0 times when n is 0. (You still need to reset i to 0 before the second loop though.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks A Lot Sir! Actually I have to specifically code using do..while loop hence I can't use while hence for the problem about input n=0 I'll have to use if-else. – meisner97 Sep 23 '18 at 18:09
0

U need to reset the value of i :

do
{
    a[i] = sc.nextInt();
    i++;
} while (i<n);
i =0;
do
{
    if(a[i]%2==0)
        evenCount++;
    else 
        oddCount++;
    i++;
} while (i<a.length);