How to Add Int Into a String in Java

When you deal with numbers in your Java programs, you can output them to users along with text. You can add numbers such as primitive-type integers to strings using the StringBuilder class. You can add an integer to a string directly, but since string objects are immutable in Java, this is inefficient. If an object is immutable, it cannot be altered. If you add something to a string, this causes Java to create a new string. Instead of doing this, you can use the StringBuilder class to add your integer to a text string more efficiently.

Instructions

    • 1

      Create your string variable. If you do not already have a string to which you want to add an integer, add the following line to your program:
      String someText = "Here is a number";

      You can choose any variable name and string value you like. If you do not have an integer variable yet, use the following code to create one:
      int myNumber = 5;

      Your program now has the string and the integer to add to it.

    • 2

      Create a StringBuilder object. Add the following code to create an object instance of the StringBuilder class:
      StringBuilder sBuild = new StringBuilder(someText);

      The StringBuilder constructor takes a string parameter to model initially. Once you have a StringBuilder object, you can carry out modifications on the string value, including adding values of other types such as integers to it.

    • 3

      Append additional data to your string using the StringBuilder object. Add the following line to your program:
      sBuild.append(": ");

      This prepares your string for adding the integer value, by preceding it with a colon and a space. Otherwise, your integer would be appended immediately after the existing text with no space in between. Add the following line to append your integer to the string:
      sBuild.append(myNumber);

      You can include further append-method calls if you want to add more text after the number.

    • 4

      Get the string value back from your StringBuilder object. If you want to store the new string value in the original string variable, use the following code:
      someText = sBuild.toString();

      If you would prefer to create a new string variable, leaving the original one unaffected, use the following syntax instead:
      String newText = sBuild.toString();

      You can now refer to the new string at subsequent points in your code.

    • 5

      Test your new string. Add the following code to output your string:
      System.out.println(someText);

      If you created a new string variable, alter the print-method parameter to reflect its name. Save your file and run it. Check that the output matches what you need for your program functionality. You can experiment with the code if you want to familiarize yourself with the StringBuilder class, calling its alternative methods and checking the results.

Tips & Warnings

  • If you make changes to immutable objects such as strings, your programs may use unnecessary resources.

  • If you are formatting strings for user display, make sure you test your program thoroughly.

Related Searches:

References

Resources

Comments

Related Ads

Featured