0

The code is listed here:

import java.util.Scanner;
public class InputCharacterInfo {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter a character...");
        String variableChar;
        char aChar;
        variableChar = Character.toString(aChar);
        variableChar = keyboard.nextLine();
        System.out.println("The character is " + aChar);
        if(Character.isUpperCase(aChar))
            System.out.println(aChar + " is uppercase");
        else
            System.out.println(aChar + " is not uppercase");
        if(Character.isLowerCase(aChar))
            System.out.println(aChar + " is lowercase");
        else
            System.out.println(aChar + " is not lowercase");
        aChar = Character.toLowerCase(aChar);
        System.out.println("After toLowerCase(), aChar is " + aChar);
        aChar = Character.toUpperCase(aChar);
        System.out.println("After toUpperCase(), aChar is " + aChar);
        if(Character.isLetterOrDigit(aChar))
            System.out.println(aChar + " is a letter or digit");
        else
            System.out.println(aChar +
            " is neither a letter nor a digit");
        if(Character.isWhitespace(aChar))
            System.out.println(aChar + " is whitespace");
        else
            System.out.println(aChar + " is not whitespace");
    }
}

My problem entails that I need to identify the char variable "aChar" as a String which I put as "variableChar", however I've run into a point where I do not know what to define the char "aChar" as without removing the ability of the user to input. User input is crucial for this assignment.

I've tried moving around the char aChar; statement multiple times, also tried changing the definition of "variableChar". Resulted in the char not being defined, the rest of the code not working, or the String not reading properly.

diitrii
  • 1
  • 1
  • 1
    What exactly is this code supposed to do? Please elaborate. – Unmitigated Mar 25 '23 at 08:21
  • Does this answer your question? [How to convert/parse from String to char in java?](https://stackoverflow.com/questions/7853502/how-to-convert-parse-from-string-to-char-in-java) – kaya3 Mar 25 '23 at 08:24
  • 2
    If you want to change a string to a character just use `"Your String".charAt(position)`. For Example, If you give the input to `variableChar` as `c` then you can use `variableChar.charAt(0)` to convert it into a character. – Inamul Hassan Mar 25 '23 at 08:29
  • hint: first read, then convert – kleopatra Mar 25 '23 at 08:39
  • Your code contains almost 20 uses of the value of `aChar`, but `aChar` has no value assigned to it (not counting the case where you try and convert the unassigned value to lowercase) – Arfur Narf Mar 25 '23 at 14:45
  • @ArfurNarf That's one of the issues I'm having, I don't necessarily know what to configure `aChar` as, because it needs to accept user input but if I label it as just a character it no longer accepts user input. – diitrii Mar 25 '23 at 16:54
  • @Unmitigated, the code that is supposed to read as follows: `Enter a character... R` `The character is R` `R is uppercase` `R is not lowercase` `After toLowerCase(), aChar is r` `After toUpperCase(), aChar is R` `R is a letter or digit` `R is not whitespace` This is simply an example, but it should be able to accept multiple different characters. – diitrii Mar 25 '23 at 16:55

1 Answers1

0

Your statement in the title seems backwards.

converting a char to a String for user input

You want to read a string and convert to a char.

Change this part:

    System.out.print("Enter a character...");
    String variableChar;
    char aChar;
    variableChar = Character.toString(aChar);
    variableChar = keyboard.nextLine();

to

    System.out.print("Enter a character...");
    String line = keyboard.nextLine();
    char aChar = line.charAt(0);

and that should get you started.

Note:

  1. Better name for the line, 'variableChar' needlessly tells us that a variable is a variable, and it doesn't hold a 'char' anyway
  2. Combining declaration with initialization is better style.
Arfur Narf
  • 392
  • 1
  • 5