-1

Ok so I need the variable "value" to have a different name every time the cycle repeats. I am fairly new to java so I am unsure how to go about this. Thanks for the help in advance!

for (int i = 0; i < varOptions.size(); ++i) {
       System.out.print(varOptions.get(i) + ": ");
       double value = myScanner.nextDouble();
}
Jonathan Mousley
  • 143
  • 1
  • 1
  • 5
  • Can you explain a bit more about what you need to do? You've come up with an untenable solution - you can't change the variable name so what is it you really need to do? – stdunbar Jul 12 '16 at 20:41
  • 1
    I'm not seeing the purpose of doing this as your variables lifetime exists only in the scope of the loop, so when the loop repeats, the previous instance is lost, so why not use the same name? There is a thread related to this here if you wish to read more http://stackoverflow.com/questions/7762848/increment-variable-names – J. Schei Jul 12 '16 at 20:44

2 Answers2

1

You can't do that. Maybe you can use value as an array?

double[] value = new double[varOptions.size()];
for (int i = 0; i < varOptions.size(); ++i) {
       System.out.print(varOptions.get(i) + ": ");
       value[i] = myScanner.nextDouble();
}

In array value you will have an array with all input values.

Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
0

This is a good place to use an array to store the data:

double[] someValue = new double[varOptions.size()];
for (int i = 0; i < varOptions.size(); ++i) {
    System.out.print(varOptions.get(i) + ": ");
    someValue[i] = myScanner.nextDouble();
}

After the for loop, someValue, an array of double values, contains all answers

Rabbit Guy
  • 1,840
  • 3
  • 18
  • 28