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 2
Oldest  Newest  Rating
0 Bernard Pyc0d333 Parah · January 16, 2015
it prints the line as an error message i.e the text will be in red color. @ Troy.

@c student, I am just trying it this way, I can do it using enum too.

 so are we saying there is no way to beat this?
0 Velisa Petricevic · January 17, 2015
Try this:

Right click your package, properties Java Build Path, Libraries and tell me what you have in there.
0 Kuroodo Ditory · January 17, 2015
Tbh I also have such an error. My switches don't work with Strings in eclipse. The error happens when I just type a string in a switch.

Except that it only happens sometimes. Sometimes it works, sometimes it doesn't.
0 Bernard Pyc0d333 Parah · January 17, 2015
it was empty, now it works with this when I added it and went through every single config to make sure it's Java7.
/usr/lib/jvm/java-7-openjdk-amd64
@ Velisa

ok guys thanks for all your suggestions and for being patient  with me.  I tried it from terminal after pasting it on gedit, it worked.

I appreciate the effort guys, learnt alot of new things :)
0 Bernard Pyc0d333 Parah · January 17, 2015
@Stefan Gost 
@Mike Conroy
@ Vincenzo Marcella 
@Sam Kuzio

@c Student
@Troy Jones
@Kuroodo Ditory
@Velisa Petricevic

Gracias.
-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.
  • 1
  • 2

Java / Android Development

106,967 followers
About

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

Links
Moderators
Bucky Roberts Administrator