using strings in switchcases

+1 Bernard Pyc0d333 Parah · January 15, 2015
hey guys, please check out this code and see if you can figure out why it's not working.
I want the user to enter the month, then the whole string will be switched to a lowercase and then switched.

import java.util.Scanner;
import java.lang.String;
public class switchSeason
{


public static void main(String [] args)
{
String month;
Scanner in = new Scanner(System.in);
month = in.nextLine();
String mon = month.toLowerCase();
switch (mon) //error here
{
case "january":
case "february":
case "march":
case "december":
System.out.println("This is a winter month");
break;

case "june":
case "july":
case "august":
case "september":
System.out.println("This is a summer month");
break;

default:
System.err.print("sorry check your spelling and enter the correct spelling");
}

}
}


Post a Reply

Replies

- page 1
Oldest  Newest  Rating
+1 Mike Conroy · January 15, 2015
Could I ask what version of Java you are using?
The ability to use a switch statement was only introduced in Java 7.

However, would you mind posting the error message you are receiving? As it is possible it is not caused by the above and could be something else.
+1 Bernard Pyc0d333 Parah · January 15, 2015
Java 7,
error is

Error:(16, 16) java: incompatible types
found   : java.lang.String
required: int
0 Mike Conroy · January 15, 2015


It's an important thing to note that the "Java" behind android and the "java"/"bytecode" the Dalvikvm or ART runs is not quite equivalent to the Oracle version of Java, and therefore will have some inconsistencies. Depending on what version of the dalvikvm/ART you're running, I would bet that you would see inconsistencies like this. I'm not sure how they align, but older versions of android probably don't reflect the changes in the language where new versions probably have a VM that does. This is one of the largest points of criticism for the android language, because it presents an unprecedented amount of fragmentation to the Java language.


Whilst this is true, looking at his code suggests he isn't programming for Android and is therefore using the standard Oracle version of Java - I could be wrong on this as I am not 100% familiar with Android development but I don't believe it uses a main method as a point of entry.

Java 7, 
error is

Errorhttps://www.thenewboston.com/images/emoticons/sad.png16, 16) java: incompatible types
found   : java.lang.String
required: in


That error is associated with using a version of Java that doesn't support Strings in switch statements ie. Java 6 or below.
This answer may help you:
http://stackoverflow.com/a/15894055/3169340
-1 Bernard Pyc0d333 Parah · January 15, 2015
I am not coding android, it's just a Java program trying to use strings in a switch statement.
0 Vincenzo Marcella · January 15, 2015
For whatever IDE you're using, there should be an option to set at which JRE you're going to be using for the project. Try creating a new project and look for an option to set the JRE to JavaSE-1.7+ and create the project. After just copy and paste your code to see if it will work then
0 Bernard Pyc0d333 Parah · January 15, 2015
javac 1.7.0_67 
0 Mike Conroy · January 16, 2015
Is it on compiling you get the error or at runtime?
I imagine it is on compiling but just want to double check.
0 Stefan Gost · January 16, 2015
are you using an IDE?
if you are using eclipse please check the the project compiler. sometimes it is set to a lower java version.

(right click on the project -> properties -> java compiler)
0 Bernard Pyc0d333 Parah · January 16, 2015
it's not a runtime error and my java compiler is set to jdk1.7.....
my IDE is Intellij Idea (jetbrains)
+1 Troy Jones · January 16, 2015
What does 

System.err.print


do?
  • 1
  • 2

Java / Android Development

107,105 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator