java - String Switch Statement -
i developing java class. can please have in using switch statement string data type.
here 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 error getting:
incompatable types - found java.lang.string expected int.
this link lead java 7: http://www.oracle.com/technetwork/java/javase/downloads/index.html
you require java 7, can use strings
in switch statement.
when create new project in ide (the ide in picture eclispse) make sure select java ends 1.7
or
just make each string correspond int
value.
Comments
Post a Comment