Skip to main content

ASCII Code in Java (Part 2)

>

The ASCII Code: (Part 2)

  • Printing the ASCII code for a character:
    // ASCII code demo
    import java.util.Scanner;
    class test {
    public static void main(String ar[]) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter the character to check the ASCII code:");
    char ch = scan.next().charAt(0); // input of character
    // We know that the (Decimal) integer value of a character is its ASCII value.
    // Hence, just coverting the character to integer will do the triick.
    int value = (int)ch;
    System.out.println("Ascii code for: "+ch+" is "+value );
    }
    }
    /* OUTPUT:
    Enter the character to check the ASCII code:
    a
    Ascii code for: a is 97
    Enter the character to check the ASCII code:
    A
    Ascii code for: A is 65
    */
    view raw pattern.java hosted with ❤ by GitHub
  • Printing the character based on the ASCII code input (Reverse of the previous program):
    // ASCII code demo
    import java.util.Scanner;
    class test {
    public static void main(String ar[]) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter the integer ASCII code:");
    int value = scan.nextInt(); // input of Decimal value
    // Just covert the integer to character.
    char ch = (char)value;
    System.out.println("Character value for ASCII code: "+value+" is "+ch );
    }
    }
    /* OUTPUT
    Enter the integer ASCII code:
    67
    Character value for ASCII code: 67 is C
    Enter the integer ASCII code:
    99
    Character value for ASCII code: 99 is c
    */
    view raw pattern.java hosted with ❤ by GitHub
  • Printing the ASCII code every character in an input String:
    // Print the ASCII values for all the characters present in the input string entered by user
    import java.util.Scanner;
    class test {
    public static void main(String ar[]) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter the string:");
    String str = scan.next();// input a string.
    // As string is combination of different characters, we need to extrac
    // individual character and then print the ASCII respectivly.
    for(int i=0;i<str.length();i++)
    {
    char ch = str.charAt(i); // extract individual characters by charAt method
    int value = (int)ch;
    System.out.println(ch+" ASCII code is "+value);
    }
    }
    }
    /* OUTPUT
    Enter the string:
    India
    I ASCII code is 73
    n ASCII code is 110
    d ASCII code is 100
    i ASCII code is 105
    a ASCII code is 97
    */
    view raw pattern.java hosted with ❤ by GitHub
Click HERE for ASCII Code Information (Part 1)
Click HERE for the Methods of Character class.

Comments

Relevant to YOU:

Popular posts from this blog

ASCII Code in Java (Part 1)

> The ASCII Code: (Part 1) ASCII stands for "American Standard Code for Information Interchange". As you may remember (Grade 9), computers only work with HIGH(1) and LOW(0) electrical states, known as bits, with correspond to mathematical Base 2 numbers. That is, computers only understand binary language. ASCII codes represent text (or other things) in computers. Assume that you are working with MS Word, or PPT or any other tool that uses text based inputs from user. You need to type a sentence that computer is not aware of. ASCII codes help to exchange this information between user and computer. When you type a character, it is converted into ASCII code first and then into Binary, which makes the computer understand what is to be typed. Hence every key on the keyboard has a specific ASCII code which is used to make the computer understand our language. If you press 4 from keyboard, then keyboard send the value 100 (value equival...

Characteristics of Java OR Java Buzzwords (Part 2)

> Java Properties: (Part 2) Robust: Java is robust because it is more reliable. It ensures the reliability by providing early checking for possible errors. It eliminates error causing constructs such as pointers. Java restricts the programmers in a few key areas, to force them to find mistakes early in program development. At the same time, Java frees from having to worry about many of the most common causes of programming errors. Because Java is a strictly typed language, it checks the code at compile time. However, it also checks your code at run time. Consider two main reasons of program failure: Memory management mistakes and Mishandled exceptional conditions (run time errors). Memory management is difficult and tedious in c/c++, the programmers have to manually allocate and free memory. This sometimes leads to problems, because programmers will either forget to free memory that has been previously allocated or, worse, try to ...

Character Class in Java (Part 2)

> The Character Class: (Part 1) Methods of Character class: Example: Method -> isDigit() [validating name] Example: Method -> isLetter() [validating mobile number] Example: Method -> isLetterOrDigit() [validating user_id / username] Example: Method -> isLowerCase() Example: Method -> isUpperCase() Example: Method -> toUpperCase() and toLowerCase() Click HERE for the list of Methods of Character class. Click HERE for the ASCII Table for different Characters