I am developing a Java Class. Can I please have some help in using a Switch statement with a String data type.
Here is my code:
String testString = "Nut";
switch (testString)
{
case "Nut":
if(NutCount < NutMaxCount)
NutCount += 1;
break;
case "Caramel":
if(CaramelCount < CaramelMaxCount)
CaramelCount += 1;
break;
case "Chocolate":
if(ChocolateCount < ChocolateMaxCount)
ChocolateCount += 1;
break;
case "Marzipan":
if(MarzipanCount < MarzipanMaxCount)
MarzipanCount += 1;
break;
}
This is the error I am getting:
incompatable types - found java.lang.String but expected int.
This link will lead you to Java 7: http://www.oracle.com/technetwork/java/javase/downloads/index.html
You will require Java 7, so you can use Strings
in a switch statement.
When you create a new project in your IDE (the ide in the picture is eclispse) make sure you select a Java that ends with 1.7
or
Just make each string correspond to a int
value.