Home > Software engineering >  Converting a user inputted integer to the character with that ASCII code
Converting a user inputted integer to the character with that ASCII code

Time:11-05

This is my first post. I also just started. We are supposed to use method calling to get an integer from the user and then use the value inputted in another method and convert it into a ASCII character. This is what I have -

public static void main(String[] args) {
    System.out.println(
            "*** You will enter values in the range of 33-126 for the whole numbers and 33.0-126.0 for the real numbers***");

    System.out.println("");

    System.out.println("The number is "   getWholeNumber()   
                       " and the character for this is a(n) "   printCharacter());
}

public static int getWholeNumber() {
    int getWholeNumber;
    System.out.println("Enter a whole number, one that does not have a decimal point: ");
    Scanner input = new Scanner(System.in);
    getWholeNumber = input.nextInt();
    return getWholeNumber;
}

public static void printCharacter(int getWholeNumber) {
    char letter = (char) getWholeNumber();
    System.out.println(letter);
}

I have successfully called the method and displayed the wholenumber but cannot seem to figure out how to convert that to the ASCII character represented by the number.

CodePudding user response:

If you first convert the int to a char, you will have your ascii code.

For example:

int iAsciiValue = 9; // Currently just the number 9, but we want 
 //Tab character
// Put the tab character into a string
String strAsciiTab = Character.toString((char) iAsciiValue);

CodePudding user response:

At first you must convert int to char. Then use Character.toString(c) to get ASCII value of the code:

public static void showAsciiCode(int code) {
    char c = (char)code;
    String ascii = Character.toString(c);
    System.out.println("code="   code   "; char="   ascii);
}

Output:

code=33; char=!
code=34; char="
code=35; char=#
code=36; char=$
...

CodePudding user response:

There are some problem with your code:

  1. Do not use the same name for the method and for the variable. You have method getWholeNumber and variable with the same name. For variable use other name like result.

  2. When you write " and the character for this is a(n) " printCharacter()); you call printCharacter() method, but this method do not return any value. In its body you have already printed result, so I changed this method to getCharacter()

Changed code:

import java.util.Scanner;

class ascii {

    public static int getWholeNumber() {
        int result;
        System.out.println("Enter a whole number, one that does not have a decimal point: ");
        Scanner input = new Scanner(System.in);
        result = input.nextInt();
        return result;
    }

    public static char getCharacter(int code) {
        char letter = (char) code;
        return letter;
    }


    public static void main(String[] args) {
        System.out.println("*** You will enter values in the range of 33-126 for the whole numbers and 33.0-126.0 for the real numbers***");
        System.out.println("");
        int code = getWholeNumber();
        char ascii = getCharacter(code);
        System.out.println("The number is "   code   " and the character for this is a(n) "   ascii);
    }

}

EDIT:

If you have to print letter from printCharacter() method then you can write code like:

    public static void printCharacter(int code) {
        char letter = (char) code;
        System.out.println(letter);
    }

    public static void main(String[] args) {
        System.out.println("*** You will enter values in the range of 33-126 for the whole numbers and 33.0-126.0 for the real numbers***");
        System.out.println("");
        int code = getWholeNumber();
        System.out.print("The number is "   code   " and the character for this is a(n) ");
        printCharacter(code);
    }
  •  Tags:  
  • java
  • Related