Wednesday, July 1, 2015

// // Leave a Comment

Program To Display The Integer Equivalent Of Characters

The program below is the answer to Deitel's Java How to Program (9th Edition) Chapter 2 Exercise 2.29.

Question: Write an application that displays the integer equivalents of some uppercase letters, lowercase letters, digits and special symbols. Display the integer equivalents of the following: A B C a b c 0 1 2 $ * + / and the blank character.


/**
 *
 * @Author: Aghatise Osazuwa
 * Website: www.cscprogrammingtutorials.com
 *
 * Exercise 2.29 - Integer Value Of A Character
 * This Program Displays The Integer Equivalents Of Some Uppercase Letters, 
 * Lowercase Letters, Digits And Special Symbols
 *
 */ 


public class Ex02_29 {
    public static void main (String [] args) {

       System.out.printf("The character %c has the value %d\n", 'A', ((int) 'A' ));
       System.out.printf("The character %c has the value %d\n", 'B', ((int) 'B' ));
       System.out.printf("The character %c has the value %d\n", 'C', ((int) 'C' ));
       System.out.printf("The character %c has the value %d\n", 'a', ((int) 'a' ));
       System.out.printf("The character %c has the value %d\n", 'b', ((int) 'b' ));
       System.out.printf("The character %c has the value %d\n", 'c', ((int) 'c' ));
       System.out.printf("The character %c has the value %d\n", '0', ((int) '0' ));
       System.out.printf("The character %c has the value %d\n", '1', ((int) '1' ));
       System.out.printf("The character %c has the value %d\n", '2', ((int) '2' ));
       System.out.printf("The character %c has the value %d\n", '$', ((int) '$' ));
       System.out.printf("The character %c has the value %d\n", '*', ((int) '*' ));
       System.out.printf("The character %c has the value %d\n", '+', ((int) '+' ));
       System.out.printf("The character %c has the value %d\n", '/', ((int) '/' ));
       System.out.printf("The Blank character (' ') has the value %d\n", ( (int) ' ' )));

    }
}

The program below is a modified version of the above such that the user can enter a character and the program displays its integer equivalent.


import java.util.Scanner;

public class Modified_Ex02_29 {
    public static void main (String [] args) {

        Scanner input = new Scanner (System.in);

        System.out.print("Enter a character: " );
        char character = input.next().charAt(0); //read only the first character entered
        System.out.printf("The character %c has the value %d\n", character, ((int) character ));

    }
}


Click here to see other answers to Java How to Program.

0 comments:

Post a Comment


DISCLAIMER: Opinions expressed in comments are those of the comment writers alone and does not reflect or represent the views of the post author. We reserve the right to delete any post deemed inappropriate or offensive and/or spammy. Please do not use abusive words/hate speech.

I Appreciate your valuable Feedback. So, Please DO NOT SPAM - Spam comments will be deleted immediately.

Don't use brand name in name field and you're not allowed to use links in comments unless it's necessary. Such comments will be removed immediately.

Thanks.