0

I create a file, and roll a dice which is saved on each row. Then I read the file, and save the dice rolls as an array.

I loop through the array and count how many 1,2,..,6 I get. These are called r1,r2,..r6 and are int's.

Now I want to create a new array with these ints (r1..r6).

I was thinking something like:

int [] sums = new int[6];
for (int i = 0; i < sums.length; i++) {
sums[i] = r(i+1); //get r1,r2,..,r6
}

This obviously doesn't work. I have tried to search how to do this. Can't figure it out.

Help?

note: I know since it is just six variables I can do

sums[0] = r1;
sums[1] = r2;
...
sums[5] = r6;

But I want to learn something new and do it with a loop (:

Thank you!

4
  • 2
    Count directly into the sums array and delete the r-variables. Commented Oct 19, 2020 at 17:38
  • Yeah, that's what I did since I couldn't do this. Do you imply I cant do what I'm asking? Commented Oct 19, 2020 at 17:45
  • Variable names aren't, in general, available at runtime. So computing the names of the variables you want as your code runs will not be useful. Method names are available, and you can invoke methods by name with reflection, but it's rarely a good idea. If the r-variable were fields they could be accessed by reflection, but that's still a poor approach. Commented Oct 19, 2020 at 17:52
  • @TomHawtin-tackline ok, thank you! Commented Oct 19, 2020 at 17:54

3 Answers 3

1

You can do this via reflection. Here's a simple example:

import java.lang.reflect.*;

class Foo {
  private int r1, r2, r3, r4, r5, r6;

  void run() throws Exception {
    r1 = 11; r2 = 22; r3 = 33; r4 = 44; r5 = 55; r6 = 66;

    for(int i=1; i<=6; i++) {
      String name = "r" + i;
      int value = getClass().getDeclaredField(name).getInt(this);
      System.out.println("The value of " + name + " is " + value);
    }
  }

  public static void main(String[] args) throws Exception {
    new Foo().run();
  }
}

This assumes the variables are object members. Getting local variables by name is way harder.

This should only ever be used for fun. Using indexed variable names is slow, error prone, and shows a gross misunderstanding of how Java works.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, I guess this was what I was looking for. "gross missunderstanding of how Java works" - sounds like me :D I ended up updating the sum-array in the loop where I count the rolls.
Glad to hear it. That's a much cleaner, easier, safer, and faster approach in all respects.
1

Basics: You can loop on arrays. So, let's create one.

Define an array R like:

int[] R = new int[]{ r1, r2, r3, r4, r5, r6 };

Now use it in the loop like:

sums[i] = R[i];

2 Comments

R is a terrible variable name. "roll" would be much better.
weeelll.. the problem is that I want an loop to assign the value of the array. With this I still do it "manually".
1

You used wrong operator in the loop. < vs >.

int [] sums = new int[6];
int[] R = new int[]{ r1, r2, r3, r4, r5, r6 };

for (int i = 0; i < sums.length; i++) {
sums[i] = R[i]; //get r1,r2,..,r6
}

6 Comments

r(i+1) will not turn into the variables r1, r2, r3, etc...
Typo: R <-> r, Also "r" is an awful variable name.
And since you just updated that part of the code you should explain that in the answer as well.
oh yeah saw that now, but that was not the problem. as @takendarkk is saying, r(i+1) doesn't return value for r1,r2..r6
Come on OP, that's weird to copy! :')
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.