-1
votes
public static void main(String[] args) {
        int sumEven = 0;
        int sumOdd = 0;
        Scanner scan = new Scanner(System.in);

    

// getting user's input

        System.out.println("Enter the number:");
        int num = scan.nextInt();

//converting int number to array

        String a = Integer.toString(num);
        int[] newNum = new int[a.length()];
        for (int i=0; i<a.length(); i++){
            newNum[i] = a.charAt(i);
        }

   

// checking the element is even or odd

        for (int i = 0; i<num; i++){
            if (newNum[i] % 2 ==0){
                sumEven = sumEven + newNum[i];
            }else{
                sumOdd = sumOdd + newNum[i];
            }
        }

   

// printing the output

        System.out.println("Sum of Even Numbers: "+sumEven);
        System.out.println("Sum of Odd Numbers: "+sumOdd);
    }
4
lets take num as 25 so the array formed will be ['2', '5'] but in 2nd for loop you added num which is 25 and hence loop will run 25 times, so as newNum 's length is only 2 (['2', '5']) it would go out of bounds.Viraj D
The length of newNum is not num. Do you know how to debug your code?Abra
you may need to change int[] newNum = new int[a.length()] to int[] newNum = new int[num] as num integer user has enteredsanjeevRm

4 Answers

1
votes

In your second loop,num might not the same with the length of newNum,so just need to change below

for (int i = 0; i<newNum.length; i++){ // change from num to newNum.length
    if (newNum[i] % 2 ==0){
        sumEven = sumEven + newNum[i];
    }else{
        sumOdd = sumOdd + newNum[i];
    }

and in your first loop, make sure you can get int value correct

 String a = Integer.toString(num);
    int[] newNum = new int[a.length()];
    for (int i=0; i<a.length(); i++){
        newNum[i] = a.charAt(i)-'0'; // make sure we can get int
    }
}
0
votes

You should dry run this program.

Suppose your num = 50

a = "50" string whose length =2

then the length of newNum = 2

but you are trying to access up to 50 place in the loop that why it is coming

I think you are under standing what I am saying

0
votes

There are some mistakes in your code-

  1. newNum[i] = a.charAt(i); this will convert the integer character to it's ASCII values. In case if you type 5 it will become 53 and if you enter 6 it will become 54.

  2. When you are checking whether the element is even or odd. for (int i = 0; i<num; i++) and this is the main reason for getting ArrayIndexOutOfBounds so you need to run loop till the array length.


public static void main(String[] args) {
    int sumEven = 0, sumOdd = 0;
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter the number:");
    int num = scan.nextInt();
    String a = Integer.toString(num);
    int[] newNum = new int[a.length()];
    for (int i=0; i<a.length(); i++){
        newNum[i] = Integer.parseInt(String.valueOf(a.charAt(i)));
    }
     for (int i = 0; i<newNum.length; i++){
        if (newNum[i] % 2 == 0){
            sumEven = sumEven + newNum[i];
        }else{
            sumOdd = sumOdd + newNum[i];
        }
    }
    System.out.println("Sum of Even Numbers: "+sumEven);
    System.out.println("Sum of Odd Numbers: "+sumOdd);
}
0
votes

What you are doing wrong is

  • for (int i = 0; i<num; i++){ if (newNum[i] % 2 ==0){*

What's happening : for example user enters num=19, the above for loop is supposed to run 19 times, and for each time in loop, you are using 'i' as index of array newNum which is of size 2 only. so when the loop reaches index 2, it gives you error.

You can : either post the full problem so I can help you with what you are trying to achieve OR you can try to adjust your code according to your needs as the error is identified.