0

I need to insert an Integer variable (myLonString) in a String variable (myBufferSendTrama) in Java

Integer myLonString;
String valoOfLongOfString = Integer.toString(lonString);
myBufferSendTrama = "{\"f\":\"valoOfLongOfString\"}";     //ERROR

If the integer variable equals 20, it should look like this:

myBufferSendTrama = "{\"f\":\"20\"}";
enzo
  • 9,861
  • 3
  • 15
  • 38
w1ll
  • 63
  • 1
  • 6

4 Answers4

0

You need to concatenate both strings, like:

String s1 = "jon";
String s2 = " doe";

String s3 = s1 + s2; // "jon doe"

Check out this page for more information

Tales
  • 36
  • 1
  • 4
0

You could use String.format("{\"f\":\"%s\"}", valoOfLongOfString).

More information can be found in the docs.

Martin Niederl
  • 649
  • 14
  • 32
0

Depending on your version of Java, you can make use of """ String literals and String.format() to achieve the effect fairly cleanly.

Example:

int value = 20;
String out = String.format("""
    Value is "%d".
    """, value);
System.out.println(out);

Output

Value is "20".
Tim Hunter
  • 826
  • 5
  • 10
0

No need to convert the Integer to a String, just do:

Integer myLonString;
myBufferSendTrama = String.format("{\"f\":\"%d\"}",(int) myLonString);
Dharman
  • 30,962
  • 25
  • 85
  • 135