15,920,896 members
1.00/5 (1 vote)
See more:
multiple numbers are given by user
for a given number if its number count is even sum all the numbers at even place
If number count is odd sum all the numbers at odd place
Then sum the results for all the inputs and print
Ex: enter size
5
Enter numbers
123(1+3=4)
2536(5+6=11)
2(2)
57(7)
76542(7+5+2=14)
Output: 38 (4+11+2+7+14)

I am getting Array Index out of Bounds error. How do i solve this?

What I have tried:

```import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int size=sc.nextInt();
System.out.println("Enter numbers: ");
int a[]=new int[size];
int sume=0,sumo=0,r;
for(int i=0;i<size;i++)
{
a[i]=sc.nextInt();
}
for(int i=0;i<size;i++)
{
for(int j=1;a[j]!=0;j++,a[j]=a[j]/10)
{
r=a[j]%10;
if(a.length%2==0)
{
sume=sume+r;
}
else
{
sumo=sumo+r;
}
}

}
double sum=sume+sumo;
System.out.println(sum);
}
}```
Posted
Updated 10-Nov-20 18:40pm
v5
Sandeep Mewara 10-Nov-20 0:19am
Use Improve Question and update your code. Currently, it's incomplete.

## Solution 1

Java
```int count=0;
while(a[j]!=0)
{
a[j] = a[j]/10;
count++;
}
if(count%2==0) // at this point, a[j]==0
{
sume=sume+a[j]%10; // so sume stay 0
}
else
{
sumo=sumo+a[j]%10; // so sumo stay 0
}
```

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

jdb - The Java Debugger[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.

CPallini 10-Nov-20 1:58am
5.
Patrice T 10-Nov-20 2:04am
Thank you

## Solution 2

Look at what you are doing:
Java
```while(a[j]!=0)
{
a[j] = a[j]/10;
count++;
}
```
What is the value of `a[j]` after the loop?
Read the question again, and think about what it is asking you to do. How would you do it manually?
This may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 OriginalGriff 70 k5054 65 Jo_vb.net 60 Pete O'Hanlon 50 GamersLube LLC. 20
 Pete O'Hanlon 960 OriginalGriff 861 Dave Kreskowiak 475 Richard MacCutchan 320 Richard Deeming 280

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900