-1

i have a method with signature int but the compiler hints me to return c which is type char.

1/ how is it possible to return char variable in this case

//scissors , paper , rock are static final in gamelogic class

public int getInput(){  
    String choice;
    char c;
    do {
        System.out.println("Make a choice");
        choice  = sc.nextLine().toUpperCase();
        c = choice.charAt(0);

        if(c == 'P'){
            return gameLogic.paper;
        }else if(c == 'R'){
            return gameLogic.rock;
        }else if(c== 'S'){
            return gameLogic.scissor;
        }
    }while(c != 'S' && c != 'R' && c != 'P');
    return c;
}
  • Possible duplicate of [How can I convert a char to int in Java?](https://stackoverflow.com/questions/46343616/how-can-i-convert-a-char-to-int-in-java) – UninformedUser Jul 13 '19 at 03:17
  • Please use the search engine next time ... https://stackoverflow.com/questions/46343616/how-can-i-convert-a-char-to-int-in-java/46343671 – UninformedUser Jul 13 '19 at 03:17
  • 2
    it's not the same point! – Khalil Beldi Jul 13 '19 at 03:20
  • Same reason you can write `public double foo() {return 1;}`. – user2357112 Jul 13 '19 at 03:22
  • @AKSW: That question has nothing to do with this one. At best, the title sounds like it could be related, but the actual content is completely unrelated. – user2357112 Jul 13 '19 at 03:24
  • `public char getInput()` – c0der Jul 13 '19 at 03:33
  • 1
    **Unrelated:** Your `while` statement will always be `true` (because it is only reached if P, R, and S are not selected). Perhaps you mean to `return c;` as the final `else` of your `if` statement? – Zephyr Jul 13 '19 at 03:39
  • But to answer your question, a `char` is really just a pointer to a character's location in an ASCII table, which is an integer. For example, the `char` "k" is ASCII #75. So if the user were to type "k," the method would return `75`. – Zephyr Jul 13 '19 at 03:42
  • 1
    **Unrelated 1:** Java naming convention is for class names to start with uppercase letter, and for constants (`static final` fields) to be all uppercase, so your code should be `return GameLogic.PAPER;`. --- **Unrelated 2:** This is a great example of code that should use an `enum`. You should have a `HandShape` enum type with `ROCK`, `PAPER`, and `SCISSOR` as enum values, and your methods return type should be `HandShape` using `null` for the final `return` statement, or the return type should be `Optional`, using `Optional.empty()` for the final `return` statement. – Andreas Jul 13 '19 at 03:44
  • 1
    @Zephyr Please watch your terminology. It is the character *code*, or maybe the *index*, for a character set. It is certainly not a *pointer*, since that term has other meanings. – Andreas Jul 13 '19 at 03:46
  • @Andreas - Just trying to explain it in layman's terms. I missed the chance to edit my comment by a couple of seconds, but you are correct, it is a "code or index that indicate's a character's location in the character set." – Zephyr Jul 13 '19 at 03:49

3 Answers3

1

A char is really just an unsigned 16-bit integer. An int is a 32-bit integer. Therefore, every char value will fit in an int, with no loss of information.

This known as a widening primitive conversion. From the Java Language Specification:

A widening conversion of a char to an integral type T zero-extends the representation of the char value to fill the wider format.

In your case, the “integral type T” is int.

VGR
  • 40,506
  • 4
  • 48
  • 63
-1

changed return c; to return Character.getNumericValue(c);

public int getInput(){  
    String choice;
    char c;
    do {
        System.out.println("Make a choice");
        choice  = sc.nextLine().toUpperCase();
        c = choice.charAt(0);

        if(c == 'P'){
            return gameLogic.paper;
        }else if(c == 'R'){
            return gameLogic.rock;
        }else if(c== 'S'){
            return gameLogic.scissor;
        }
    }while(c != 'S' && c != 'R' && c != 'P');
    return Character.getNumericValue(c);
}
Dean Van Greunen
  • 5,060
  • 2
  • 14
  • 28
-1

You can return char value and it will be automatically converted to int by java. compiler would indicate to return a value at the end as all the returns are based on condition