Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialISAIAH S
1,409 PointsIsaiah.java:18: error: incompatible types: String cannot be converted to char char lotsOfStuff =...
My code:
import java.io.Console;
public class Isaiah {
public static void main(String[] args) {
Console console = System.console();
String isName = console.readLine("Enter your name: ");
String fatal = "";
if(isName.equals("*ISAIAH*")) {
String secretCode =console.readLine("YOU HAVE ENTERED A SECRET CODE.\nHERE ARE YOUR OPTIONS.\n >IF YOU WANT TO CONTINUE:\nPRESS I.\n >IF YOU WANT TO EXIT:\n PRESS E.\n ");
if(secretCode.equalsIgnoreCase("E")){System.exit(0);
}else if(secretCode.equalsIgnoreCase("I")){
fatal = console.readLine(" WARNING:\nTHIS PROGRAM CAN BE FATAL.\nDO YOU STILL WANT TO CONTINUE?\n\n ");
if(fatal.equalsIgnoreCase("yes")){
console.readLine("Hello, %s.\n", isName);}else{System.exit(0);}}}
String number = console.readLine("Enter your favorite number: ");
String mood = console.readLine("Enter your favorite mood: ");
String creature = console.readLine("Enter your favorite creature: ");
String color = console.readLine("Enter your favorite color: ");
char lotsOfStuff = number + mood + creature + color;
Character.toLowerCase(lotsOfStuff);
String isFavoriteThing = console.readLine("Is a %s, %s %s year old %s your favorite thing? ", mood, color, number,creature);
if(isFavoriteThing.equalsIgnoreCase("no")) {console.readLine("Why not? ");
console.printf("Well you should, it's all of your favorite stuff.\n\n");}
String computer = console.readLine("-------------------------------------------------\n\n Do you want to have a talk with the computer? ");
console.printf("\n---------------------------------------------\n\n\n");
String howYouDoing = "";if(computer.equalsIgnoreCase("yes")){
howYouDoing = console.readLine("Hello %s, how are you doing?\n ", isName);
if(howYouDoing.equalsIgnoreCase("sad")) {console.readLine("Why are you %s, %s? \n", howYouDoing, isName);}}}}
My error:
Isaiah.java:18: error: incompatible types: String cannot be converted to char
char lotsOfStuff = number + mood + creature + color;
^
1 error
Thanks, ISAIAH S
2 Answers
Alexandru Doru Grosu
2,753 PointsWell that is expected.
Remember what I told you before, char
(including its wrapper, Character
) and String
are two different types.
Character.toLowerCase(lotsOfStuff);
Now you are trying to call a method that expects an argument of type char
to be passed (i.e. a single character), with an argument that is of type String
.
If you wish to convert a String
to lower case, you will have to use the String
method intended for this which is also called toLowerCase
, however, you would have to call it on the String
itself like so:
lotsOfStuff.toLowerCase();
Note: toLowerCase()
will not modify the original String
, but will instead return a copy of the original String
but with all its contents in lower case. So if you want to make lotsOfStuff
lower you would have to assign it to itself, like this:
// ...
String hello = "I AM UPPERCASE";
hello = hello.toLowerCase();
console.printf("%s", hello); // Will print "i am uppercase"
// ...
This is essentially saying:
Hey, I have my string stored in hello
, so I am going to call toLowerCase()
on hello
to give me a copy of hello
but with everything in lower case. After that I will assign this value back to hello
, effectively replacing the old String
which might have had uppercase characters.
Alexandru Doru Grosu
2,753 PointsYou get the error because char
(s) and String
(s) are two different things. A char
, as the name implies, stores a single character. A String
(as the name also sort of implies) is a series of characters. Strings are denoted by " " while chars are denoted by ' ';
So for example the String
String hello = "Hello World!";
is made out of the individual char
(s)
'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd'
The problem with your code is that you are concatenating (by using the + sign) four Strings, and you are trying to store the result of that (which is also a String) into a single character. You are trying to put a bunch of apples into a basket that is only meant to hold one apple essentially.
TL;DR char
can store only one character and you are trying to cram a String
into it, which is a series of characters.
So to fix your problem, lotsOfStuff
has to be of the type String and not char.
ISAIAH S
1,409 PointsWhen I changed:
char lotsOfStuff = number + mood + creature + color;
to:
String lotsOfStuff = number + mood + creature + color;
I got the following error:
Isaiah.java:19: error: no suitable method found for toLowerCase(String)
Character.toLowerCase(lotsOfStuff);
^
method Character.toLowerCase(char) is not applicable
(argument mismatch; String cannot be converted to char)
method Character.toLowerCase(int) is not applicable
(argument mismatch; String cannot be converted to int)
1 error