-1
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);
    }
knittl
  • 246,190
  • 53
  • 318
  • 364
  • 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 Doshi Jul 09 '21 at 05:12
  • 3
    The length of `newNum` **is not** `num`. Do you know how to [debug](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) your code? – Abra Jul 09 '21 at 05:13
  • you may need to change `int[] newNum = new int[a.length()]` to `int[] newNum = new int[num]` as `num` integer user has entered – sanjeevRm Jul 09 '21 at 05:17

4 Answers4

1

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
    }
}
flyingfox
  • 13,414
  • 3
  • 24
  • 39
  • @VirajD this will work fine the character will be converted to ASCII number and Zeroes ascii value would be subtracted from it.ex- If user enters 5 then its ASCII is 53 so 5 = 53 - 48(ASCII of 0) – Ashish Mishra Jul 09 '21 at 05:31
  • @AshishMishra I just checked adding an integer and a String in java and it not as you mentioned. It converts the integer to String (rather than converting the chr to ASCII number) and then adds both the strings, check again – Viraj Doshi Jul 09 '21 at 10:28
  • @VirajD Foremost we are adding Integer and A character not a string, try running these ->>> ```System.out.println(10+'5'); System.out.println(100-'6');``` – Ashish Mishra Jul 09 '21 at 10:38
1

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);
}
Ashish Mishra
  • 704
  • 1
  • 6
  • 20
0

You should dry run this program.

Suppose your num = 50 

a = "50" ;

//var a is a string  whose length =2 
//then the length of newNum is 2 

newNum = 2;

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

I think you are understanding what I am saying.

Yeshwin Verma
  • 282
  • 3
  • 16
0

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.