1

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.

Abra
  • 19,142
  • 7
  • 29
  • 41
Regis
  • 15
  • 4
  • If you are allowed to use class `java.lang.Character` then see method [toString(int)](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Character.html#toString(int)) – Abra Nov 05 '21 at 05:20
  • Does this help? [How to convert ASCII code (0-255) to its corresponding character?](https://stackoverflow.com/questions/7693994/how-to-convert-ascii-code-0-255-to-its-corresponding-character) – Abra Nov 05 '21 at 05:22
  • Are you expecting your users to input a single-digit integer? – ernest_k Nov 05 '21 at 05:28
  • I am not sure I know the instructions say I have to call a void method named printCharacter() with exactly one parameter. So thats what I tried to do. – Regis Nov 05 '21 at 05:30
  • @ernest_k I am just to assume that they will input a number that is within the listed range of 33-126 – Regis Nov 05 '21 at 05:32
  • The easiest hack is `System.out.println((int) Integer.toString(getWholeNumber).charAt(0));`, which assumes a single digit was captured by the user. – ernest_k Nov 05 '21 at 05:34
  • Then it's unclear what you're trying to do. Are you getting a digit that you want to print the ASCII code of? Or are you getting an ASCII code to print the corresponding character of? – ernest_k Nov 05 '21 at 05:37
  • I am trying to take the getWholeNumber variable and turn it into ASCII. – Regis Nov 05 '21 at 05:43
  • @ernest_k look at the accepted range - 33 to 126 - which are the ASCII codes for all the printable, non-whitespace characters. Obviously the user enters an ASCII code and the program prints the character that has that ASCII code. – Abra Nov 05 '21 at 05:44
  • Then please edit your post. Your description and title say the opposite of what you actually want. All you need to do is change the statements `char letter = (char) getWholeNumber();` to `char letter = (char) getWholeNumber;` (use the parameter rather than asking for input again). Also, your main method should send the parameter: `int wholeNumber = getWholeNumber(); System.out.println("The number is " + wholeNumber + " and the character for this is a(n) " + printCharacter(wholeNumber));` – ernest_k Nov 05 '21 at 05:49
  • I am not allowed to have a variable named wholeNumber. I have very specific instructions. I have these two methods getWholeNumber, and printCharacter. The getWholeNumber method is allowed no parameters, and the printCharacter Method is allowed one parameter getWholeNumber. I have to call both of these methods in the `System.out.println("The number is " + getWholeNumber(); + " and the character for this is a(n) " + printCharacter());` it has to look exactly like that. I am stuck on the printCharacter method and how to correctly call the method that will output an ASCII character. – Regis Nov 05 '21 at 06:19

3 Answers3

1

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);
rem
  • 72
  • 3
  • 13
0

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=$
...
Michał Niklas
  • 53,067
  • 18
  • 70
  • 114
  • I think this is exactly what I was struggling to figure out. The only problem is I still am not sure how to call the method without it returning an error. – Regis Nov 05 '21 at 06:29
0

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);
    }
Michał Niklas
  • 53,067
  • 18
  • 70
  • 114
  • I honestly would do this if I could but the printCharacter method has too be void. Its the reason I am struggling so much I have no freedom to write the code any way other than what my teacher perceives as correct for this assignment. – Regis Nov 05 '21 at 07:51
  • I edited answer with printing letter from `printCharacter()` method. – Michał Niklas Nov 05 '21 at 07:59