Re: switch with character
I'm confused...
Code :
public class Switch {
public static void main(String[] args) {
Scanner sin = new Scanner(System.in);
char x = sin.next().charAt(0);
switch(x){
case 'a': System.out.println("WOW 'a'"); break;
default: System.out.println("try 'a' ;)"); break;
}
}
}
Re: switch with character
no chris.. i saw something like that in switch statement and some of the variables were enum.. i dont remember exactly the code.. but i think this is where i saw that code..
Re: switch with character
Code :
public class SomeClass
{
public static enum Days {SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY}
public static void main(String[] args)
{
Days a = Days.SUNDAY;
switch(a)
{
case Days.SUNDAY:
System.out.println("Today is sunday");
break;
case Days.Saturday:
System.out.println("Today is saturday");
break;
default:
System.out.println("Today is some weekday");
}
}
}
Re: switch with character
YAH its exactly like that!!!! tnx helloworld
Re: switch with character
I take it you wrote that from the top of your head helloworld? :)
// Json
Re: switch with character
there an error here... cant figure it out..
Code :
public class SomeClass
{
public static enum Days {SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY}
public static void main(String[] args)
{
Days a = Days.SUNDAY;
switch(a)
{
case Days.SUNDAY:
System.out.println("Today is sunday");
break;
case Days.Saturday:
System.out.println("Today is saturday");
break;
default:
System.out.println("Today is some weekday");
}
}
}
Code :
case Days.SUNDAY: //<< an enum switch case label must be the unqualified name of an enumeration constant
Code :
case Days.Saturday: //<< duplicate case label, an enum switch case label must be the unqualified name of an enumeration
Re: switch with character
Thats what I was commenting on above as well.
When using enums in switch statments you are not allowed to use the fully qualified name of the enum values.
So the code block above should look like this.
Code :
public class SomeClass
{
public static enum Days {SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY}
public static void main(String[] args)
{
Days a = Days.SUNDAY;
switch(a)
{
case [b]SUNDAY[/b]:
System.out.println("Today is sunday");
break;
case [b]SATURDAY[/b]:
System.out.println("Today is saturday");
break;
default:
System.out.println("Today is some weekday");
}
}
}
// Json
Re: switch with character
oh no! i forgot to ask how to make an input for it... big sorry for this..:eek:
Re: switch with character
Make an input?
Code :
Days days = Days.valueOf("SUNDAY");
Make sure the string you pass into the valueOf method is the same case as the enums you've reclared.
// Json
Re: switch with character
I'm not sure if any of you have noticed but if you look at the bottom of this thread, there is a 'Similar Threads' box. I'm sure this will come in handy sometimes!
Re: switch with character
weee tnx for all your help!! yehey! :-*
Re: switch with character
oh men.... i didnt notice it sir Java... anyway big thanks to you!!
Re: switch with character
so i was ryt what im looking for was posted in some of the threads here.. i just coudlnt find it .. i was looking at the tutorials but i didnt find it... haha i forgot that it was sir json's post...
Re: switch with character
:P A lot of the code I post is off the top of my head. It takes too much time to open up eclipse and type the same thing when I could just do it here. Yeah, I forgot you don't need the full quantifier for enums in switch statements. You do need them in any other conditionals, though.
Re: switch with character
Damn switches to hell! They always were a bizzare feature in programming languages imo.