-1

I want to create 16 different variables named numx, x representing the numbers 1-16. I've tried this loop

for (int x = 16; x > 0; x--){
      
    }

but I can't figure out what to put inside the loop to make it work. To define each number, I would want numx = fullNum%10 followed by

fullNum -= numx;
fullNum /= 10;

so the loop could move on to the next variable with one less digit (i.e. 16-15, 15-14, 14-13, etc).

Daniel Tam
  • 41
  • 9
  • 1
    You can't create variables like this. You can, however, create an `ArrayList` or just a plain old `Array` and store your values in there... – Idle_Mind Mar 05 '21 at 16:15
  • 2
    use an array. thats what they are for – OldProgrammer Mar 05 '21 at 16:15
  • Ok, I'll try using an array thanks for the help! – Daniel Tam Mar 05 '21 at 16:18
  • 1
    You can't dynamiccaly assign variables in java, however what you can do is assign them by ordering them in Arrays or Lists (or different data structures). This example shows how to do it using a list: List myIntList = new ArrayList(); for (int i = 0; i < 16; i++) { Integer myVar = HERE what ever logic you want to assign to the variable; myIntList.add(myVar); } – fl0w Mar 05 '21 at 16:21

1 Answers1

3

In most languages, you can't generate variable names dynamically.

Consider using an array instead:

int[] num = new int[16];
for (int x = 16; x > 0; x--){
  num[x] = fullNum % 10
}
Nayan Hajratwala
  • 350
  • 1
  • 4
  • 11