-1
void storedata(int i) {
    if(i > 0) {
        a[size-i] = in.nextInt();
        storedata(i--);
    }
}

The above code is not terminating after taking the required number of inputs. It runs forever. How can I solve this?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Is the `in` variable, `System.in`? – Sid Dec 06 '19 at 11:38
  • Upvoted you as your a new contributor. But the code is not complete, cant understand it, post more information, like full program and description of what your expecting it to do – tgkprog Dec 06 '19 at 11:38
  • Did you debug the`i` value? It can't go in negative for sure. Check the caller of the function. Debug and let us know. – Karthik R Dec 06 '19 at 11:41
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Dec 07 '19 at 08:50
  • @tgkprog: please do not upvote merely because someone is new. This can prompt a flurry of downvotes to cancel out the unmerited score, which in turn may contribute to an automated question ban. – halfer Dec 07 '19 at 08:51

1 Answers1

2

You will always call storeData recursively with the same value, because you used

storedata(i--);

Try using

storedata(--i);

instead. That will decrement i first.

halfer
  • 19,824
  • 17
  • 99
  • 186
NickJ
  • 9,380
  • 9
  • 51
  • 74