Is it possible to use a string for a type like "int something+""+i = i"

Hello!
Is it possible to do this?

int something, something1, something2, something3, something4, something5;

for(int i = 0; i < 5; i++) {
      something+""+i = i;
}

*so it uses all 5 variables and doesnt make the variable “something” 5!, so it makes all 5 variables to 1,2,3,4 & 5!

1 Like

An IntDict container is as close as we can get in Processing:

1 Like

You can’t really build variables like this.

I think one way very similar is an array:

An array is a list.

see Array / Reference / Processing.org

This list has one name, but being a list, it has different slots (lines) that you can access via their line number (index).

int[] something=new int [5]; // array 

for (int i = 0; i < 5; i++) {
  something[i] = i;
}

printArray(something); 

println("-----------------------------------");

println(  something[2] );

println("-----------------------------------");

for (int i = 0; i < 5; i++) {
  something[i] = i*10;
}

printArray(something);

This gives you an output:



[0] 0
[1] 1
[2] 2
[3] 3
[4] 4
-----------------------------------
[0] 0
[1] 10
[2] 20
[3] 30
[4] 40

(Output similar, older version)

1 Like

As has been pointed out by gotoLoop, there is also
IntDict.

You can use a String as a lookup for an int value.
String “keys” are associated with integer values.
FloatDict and HashMap are similar.

see IntDict / Reference / Processing.org

IntDict inventory;

void setup() {
  size(200, 200);
  inventory = new IntDict();
  inventory.set("cd", 84);
  inventory.set("tapes", 15);
  inventory.set("records", 102);
  println(inventory);
  // noLoop();
  fill(0);
  textAlign(CENTER);
}

void draw() {
  background (255); 

  int numRecords = inventory.get("records");
  text(numRecords, width/2, height/2);

  int numCD = inventory.get("cd");
  text(numCD, width/2, height/2+22);
}

1 Like