-3

I wish to reset my variable i back to 100 after the first loop has completed its run.

Currently once i enter the second loop my i variable begins at 600, but i want to reset this back to 100. Help would be much appreciated.

I cant just state i = 100 again as i has already been declared.

int i = 100; 
int j = 50;

for (int index1 = 0; index1 < 4; index1++){
  for (int index = 0; index < 10; index++){
    vol1HH.add(new JTextField());
    vol1HH.get(index).setBounds(i, 50, 80, h);
    window.add(vol1HH.get(index));
    i = i + 50;
  }
  // this is where I want to reset my i variable back to 100
  vol1HH.add(new JTextField());
  vol1HH.get(index1).setBounds(i, j, 80, h);
  window.add(vol1HH.get(index1));    
  j = j + 50;
}
depperm
  • 10,606
  • 4
  • 43
  • 67
Ingram
  • 654
  • 2
  • 7
  • 29
  • 2
    where's the problem? just set it to 100: `i = 100;` simple as that –  Jul 17 '15 at 20:33
  • You can't state `int i = 100` again, just `i = 100` is fine. – Anders Jul 17 '15 at 20:55
  • Also refer to this thread, it will be useful for you: http://stackoverflow.com/questions/2614072/java-define-terms-initialization-declaration-and-assignment –  Jul 17 '15 at 21:06

3 Answers3

2

You can just state i = 100.

I think you're confusing declarations with assigning a value.

int i = 100; //This is a definition.
int i = 200; //This won't compile b/c i already exists.
i = 100; //This WILL compile because you're assigning i the value 100
Beginner
  • 48
  • 4
  • yes i see, i was using int i = 100 which is why it wouldnt work - i am a bit of a newbie to java so thanks for that – Ingram Jul 17 '15 at 20:48
0

Simply add a line i = 100; where you want to reset it. Reassigning values to a variable should be perfectly fine.

deezy
  • 1,480
  • 1
  • 11
  • 22
0

Might be a bit surprising for you, but you can do it(re-assigning the value to a variable) :-

i = 100; // this is where i want to reset my i variable back to 100

I cant just state i = 100 again as i has already been declared.

Note that you are just changing the value of i not declaring it again.

AnkeyNigam
  • 2,810
  • 4
  • 15
  • 23