How to Format a String in ASCII Java

Written by mike wallace
  • Share
  • Tweet
  • Share
  • Email

Java provides multiple methods for storing text. In programming parlance, a single discrete sequence of text is called a "string." The American Standard Code for Information Interchange is a widely used method of encoding text for electronic devices. ASCII encodes 128 different characters, including all the letters in the English alphabet. Each letter has a corresponding ASCII code. You can convert a string of text in Java to the ASCII code using the built-in Java cast function.

Skill level:

Other People Are Reading

Things you need

  • Java Development Kit and NetBeans Bundle

Show MoreHide


  1. 1

    Create a new project in NetBeans by first clicking on its icon. Then navigate to "File/New Project," which is located on the top menu bar. Select "Java Application" and hit "Enter" to create a new source code file. This source code file appears in the main editor window of NetBeans and is empty except for a main function.

  2. 2

    Define a string and assign it the word "ASCII." You can choose your own text, but the output in the last step will be different if you choose a different word. You can define a string like this:

        String str = "ASCII";
  3. 3

    Iterate through each character in the string using a "for" loop, like this:

        for(int i = 0; i < str.length(); i++) {}
  4. 4

    Convert each character in the string into an integer and print it to the console. The integer value will correspond to the ASCII code value for that letter.

  5. 5

    Run the program by pressing "F6." The output looks like this:






Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.