I want to make a string that changes depending on the variable so in my example hello and the number would have to touch, so no spaces and the x should be the number that x is
Float x;
Void draw(){
X=random(30);
Print(Hellox);
}
I want to make a string that changes depending on the variable so in my example hello and the number would have to touch, so no spaces and the x should be the number that x is
Float x;
Void draw(){
X=random(30);
Print(Hellox);
}
Hey I think what you want is that:
Float x = 0;
Void draw(){
x = random(30);
Ptintln("Hello" + x);
}
With “” we tell the Programm that the hello is not a variable but a text (a string).
With + we add the float as text to the hello (String).
If you want to have a space between hello and x then write this:
Println("hello " + x);
Some general information about strings and print (ln):
To fuse variables (any type even objects (but it would output AA_.Q77@235123)) use “+” sign.
Imagine having two strings you have to fuse. How would you do it?
String a = "Hello!";
String b = "How are you?"
String c = a + b;
println(c); //Prints: Hello!How are you?
String d = a + " " + d;
println(d); //Prints: Hello! How are you?
//the difference between print and println is that println starts in a new row. To simulate it use:
print("string+"\n"); //the same as println(); "\n" means ENTER (line break)
//now imagine having 5 numbers you have to show
int a=1,b=2,c=3,d=4,e=5; //it is the same as int a = 1; int b = 2; int c = 3;...
//you could do something like:
println(a+" "+b+" "+c+" "+d+" "+e); //but this is annoying so just do this:
pritnln(a,b,c,d,e); // "," in println is the same as + " " +
I hope this helps!
Hi @MoonAlien822,
Please note that Java (the language which is under Processing), the code you write is case sensitive meaning that a word with or without an uppercase is not treated as the same thing.
Therefore your code won’t compile (maybe it was a copy pasting error but I let you know anyway)
In Java, to concatenate (that’s the word for joining two strings in programming) a piece of text (a string) and a number, you need the +
operator:
"Hello " + x
But you might wonder how the computer knows how to add a text with a number, usually you add two numbers together like: 4 + 5
right?
In Java, there are some type conversions under the hood when you use operators like +
:
String conversion (§5.4) applies only to an operand of the binary
+
operator which is not aString
when the other operand is aString
.
In this single special case, the non-
String
operand to the+
is converted to aString
(§5.1.11) and evaluation of the+
operator proceeds as specified in §15.18.1.
(from the doc)
So your variable x
is actually getting converted to String
(text) before being concatenated to the "Hello "
string.
That’s the magic of programming!
Hi!
If, from some strange reason, you need your variable be inside the string (say, you want to use an external file containing translations or something like that) you could take this approach:
float x;
void setup(){
}
void draw(){
x=random(30);
// replacer will use regex to replace variables.
println(replacer("Hello $x"));
println(replacer("I have $x bitcoins."));
println(replacer("$x is my bitcoin balance."));
}
String replacer(String str1){
String someOtherVariable="Do the same for every variable you want to include inside strings";
String str2=str1;
// You have to put a line for every variable you want
// to be embedded inside the text
// [$] finds the dollar sign
// x finds the x after dollar (name of variable)
// \\b means the end of the word.
//
str2=str2.replaceAll("[$]x\\b",""+x);
str2=str2.replaceAll("[$]someOtherVariable\\b",
""+(someOtherVariable)
);
return str2;
}
Thanks @josephh
Adding the Processing references that I could find related to this:
:)