0

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.

Truong Van Hoc
  • 87
  • 3
  • 12
  • 2
    Sorry but it is still unclear. Could you please try to rephrase the question? – user2004685 Feb 20 '16 at 11:07
  • Try `true?(char)(65+1):(char)a`. –  Feb 20 '16 at 11:15
  • If you are saying that you want to make `//2` print `B` instead of `66` then Try: `System.out.println(true?(char)(65+1):String.valueOf(a));` – user2004685 Feb 20 '16 at 11:18
  • I mean, if the user chooses "Alphabet style". The program should print out "From node A to node B", "From node A to node C", etc. If the user chooses "Number style". The program should print out "From node 1 to node 2", "From node 1 to node 3", etc. So I use the ?: operator and the "format" variable will decide which style to be printed out. – Truong Van Hoc Feb 20 '16 at 11:48

4 Answers4

2

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));
} 
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • I think short consumes less memory than int so I use short. Is it odd to do that? – Truong Van Hoc Feb 20 '16 at 11:33
  • Yes, it is. Most machines are 64-bits these days, and most others are 32 bits, so even if you're using a short, it probably consumes 32 or 64 bits just like an int does. This is premature, useless, and even counter-productive optimization. Related: http://stackoverflow.com/questions/14531235/in-java-is-it-more-efficient-to-use-byte-or-short-instead-of-int-and-float-inst – JB Nizet Feb 20 '16 at 11:37
  • I get it now. Thank you. – Truong Van Hoc Feb 20 '16 at 11:41
1

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);
  }
}
Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75
0

Try :

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

char a=65;
System.out.println(true?(char)(65+1):a);//2
YounesM
  • 2,279
  • 15
  • 28
0

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.

AlShibli
  • 1
  • 3