简体   繁体   中英

Can't convert to char in ? : operator

I want to give user with 2 choices which are whether reading the node in alphabets or numbers. It will say something like "From node A to B" or "From node 1 to 2". So I wrote codes like the //2 code(I want to make it simple so I let the condition to be "true"). But the result is "66" instead of "B". However, the //1 code works fine. How can I wrote the code that allows me to change the letter and display correctly the way I want? Thanks.

System.out.println(true?(char)(65+1):65);//1

int a=65;
System.out.println(true?(char)(65+1):a);//2

Results

B
66

*Try to make it clearer: I want to make a loop to print out

From node A to node B
From node A to node C
From node A to node D
...

So I will write

for (int i=1;i<26;i++)System.out.println("From node A to node "+ (char)(65+i));

But I also want to make it able to print out this if user decide to:

From node 1 to node 2
From node 1 to node 3
From node 1 to node 4
...

So, I make a question about the format the user wants as a boolean format variable. And I edit the code to be

boolean format=true;//I simplify it    
for (int i=1;i<26;i++)System.out.println("From node "+(format?"A":1)+ " to node "+ (format?(char)(65+i):i));

Here is there result

From node A to node 66
From node A to node 67
From node A to node 68
...

This is not what I want. It should be "From node A to node B", etc... I changed the short type in //2 to int as recommended, but it(type) is not the point I'm asking. So please don't mind it.

I can't understand what you're trying to achieve (and why you're using short variables), but the behavior you're observing is explained in the Java Language Specification :

If one of the operands is of type T where T is byte, short, or char, and the other operand is a constant expression (§15.28) of type int whose value is representable in type T, then the type of the conditional expression is T.

That explains what you see in the first example: 65 is a constant expression of type int, (char)(65+1) is a char, and the type of the ternary expression is thus char.

Otherwise, binary numeric promotion (§5.6.2) is applied to the operand types, and the type of the conditional expression is the promoted type of the second and third operands.

That explains what you see in the second example: one is of type short and the other is of type char: they're both promoted to int and the result is thus of type int.

To do what you want to achieve, I'd do something like:

private String formatNode(int node) {
    return formatAsInteger ? Integer.toString(node + 1) : Character.toString((char) (node + 'A'));
}

// ...

for (int i = 1; i < 26; i++) {
    System.out.println("From node " + formatNode(0) + " to node " + formatNode(i));
} 

There are two problems in your code, different types for the second and third expressions in the ?:, and dead code due to the use of true . I fixed the first problem two different ways. The first println selects between two int expressions, and casts the result to char . The second cases each of the selected expressions. I fixed the second problem by using a boolean whose value the compiler does not know.

public class Test {
  public static void main(String[] args) {
    boolean test = args.length < 1;
    System.out.println((char) (test ? (65 + 1) : 65));
    System.out.println(test ? (char) (65 + 1) : (char) 65);
  }
}

Try :

System.out.println(true?(char)(65+1):65);//1

char a=65;
System.out.println(true?(char)(65+1):a);//2

If I understand your question correctly, this is what you should write:

System.out.println((char)(false?(65+1):65));

You need to cast the result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM