How to Format a String in ASCII Java

Save

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.

Things You'll Need

  • Java Development Kit and NetBeans Bundle
  • 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.

  • 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";
  • Iterate through each character in the string using a "for" loop, like this:

        for(int i = 0; i < str.length(); i++) {}
  • 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.

            System.out.println((int)str.charAt(i));
  • Run the program by pressing "F6." The output looks like this:

    65

    83

    67

    73

    73

Promoted By Zergnet

Comments

Resources

You May Also Like

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!