# Issue looping an integer array to get a max number

**URL:** <https://forum.codewithmosh.com/t/issue-looping-an-integer-array-to-get-a-max-number/3302>\
**Category:** Java\
**Created:** [March 11, 2021, 1:12pm UTC](https://forum.codewithmosh.com/t/issue-looping-an-integer-array-to-get-a-max-number/3302 "2021-03-11T13:12:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harrison](https://avatars.discourse-cdn.com/v4/letter/h/3e96dc/32.png) [@Harrison](https://forum.codewithmosh.com/u/Harrison)\
**Post date:** [March 11, 2021, 1:12pm UTC](https://forum.codewithmosh.com/t/issue-looping-an-integer-array-to-get-a-max-number/3302/1 "2021-03-11T13:12:22Z")

</div>

Hi guys, am trying to use the above method to loop through an array of int and get the maximum big number, my function loop only gets me the first value. Anyone who can assist me to figure this out?

 ![Array max loop Screenshot](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/f/fb83df352df43e5d355754d07876463e054f0d68.png)

---

<div class="post-metadata">

**Author:** ![SAM](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/sam/32/167_2.png) [@SAM](https://forum.codewithmosh.com/u/SAM)\
**Post date:** [March 11, 2021, 1:38pm UTC](https://forum.codewithmosh.com/t/issue-looping-an-integer-array-to-get-a-max-number/3302/2 "2021-03-11T13:38:23Z")

</div>

To figure it out you could use the debugger or even a dry run.  
You `return maxNumber` for “each” iteration so you’ll leave the method after the first iteration.

Also review your variables and variable names. E.g. what is `iMax` for? Did you consider that there might be only negative values in the array or none at all?

---

<div class="post-metadata">

**Author:** ![Harrison](https://avatars.discourse-cdn.com/v4/letter/h/3e96dc/32.png) [@Harrison](https://forum.codewithmosh.com/u/Harrison)\
**Post date:** [March 12, 2021, 6:10am UTC](https://forum.codewithmosh.com/t/issue-looping-an-integer-array-to-get-a-max-number/3302/3 "2021-03-12T06:10:15Z")

</div>

So I managed to debug the code and did the necessary changes it’s now working, thanks for the tip, Sam.

---

<div class="post-metadata">

**Author:** ![JerryC](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jerryc/32/859_2.png) [@JerryC](https://forum.codewithmosh.com/u/JerryC)\
**Post date:** [April 13, 2021, 10:44pm UTC](https://forum.codewithmosh.com/t/issue-looping-an-integer-array-to-get-a-max-number/3302/4 "2021-04-13T22:44:50Z")

</div>

![maxNumQuestion](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/5/583e2da1878f257c3f6e707b1437494351bb993e.jpeg)
