Ternary - Char/String

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>

https://www.codecademy.com/en/courses/learn-java/lessons/conditionals-control-flow/exercises/ternary-conditional?action=lesson_resume

<In what way does your code behave incorrectly? Include ALL error messages.>

Code doesn’t misbehave, I’m just trying to satisfy my curiosity in terms of replacing char with string, so I can have it output with “Yes” : “No”; rather than ‘Y’ : ‘N’;
Would love to know if there’s a way to do that, been stuck on this same issue for hours and Google is no help.

```

public class Ternary {
public static void main(String args) {

	int hungryLevel = 1;

	char canEat = (hungryLevel > 0) ? "Yes" : "No";
	System.out.println(canEat); 

}

}

<do not remove the three backticks above>

Why can you not declare canEat as a string to begin with?

String canEat = (hungryLevel > 0) ? "Yes" : "No";

If you must take in a char, and want to print out a string, I suppose you can do the following:

String strY = Character.toString('Y'); //where 'Y' can also be a variable of type char
String strN = Character.toString('N'); //where 'N' can also be a variable of type char
String canEat = (hungryLevel > 0) ? strY.replace("Y","Yes") : strN.replace("N","No");
1 Like

Hi, thank you for the response. I tried to replace char with String but came with errors in which it took me hours to understand, applied a fix and still didn’t work. However I just tried it now (commented out my fix) with the exact same fix and it works now, so now I’m wondering why it didn’t work yesterday considering it wouldn’t have kept me up til 5am. zz

Thank you for the latter fix. I will take my time to learn how it works.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.