Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Switch Statement - Why the Error 1

Status
Not open for further replies.

Cap2010

Programmer
Mar 29, 2000
196
CA
HI,

Below is the code, I wanted in Switch case. It gives error.
Would like to know why the error.


class grade {
public static void main(String[] arguments){
int ggrade=0;
ggrade = Integer.parseInt(arguments[0]);

switch (ggrade){
case (ggrade >= 81 && ggrade <= 100):
System.out.println(&quot; grade A&quot;);
break;

case (ggrade >= 61 && ggrade <= 80):
System.out.println(&quot; grade B&quot;);
break;

case (ggrade >= 41 && ggrade <= 60):
System.out.println(&quot; grade C&quot;);
break;

case (ggrade >= 21 && ggrade <= 40):
System.out.println(&quot; grade D&quot;);
break;
case (ggrade >= 1 && ggrade <= 20):
System.out.println(&quot; grade F&quot;);
break;
default:
System.out.println(&quot; Not Numeric&quot;);
}
}
}

Need help!!!

[sig][/sig]
 
look at the definition for a switch statement again. you have [tt]case (boolean)[/tt], when it should be [tt]case integer[/tt]. That might not make immediate sense to you, but this webpage should be pretty clear:
The switch statement is not very useful in Java- all it can do is handle integers; you can't even handle other primitive types or objects. Sorry.. [sig]<p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence."[/sig]
 
Hi!

A little correction:
... all it can do is handle integers and __chars__; ...

Bye, Otto.
[sig][/sig]
 
Look at the syntax of the switch and case
Case will take an integer argument
int i;
switch(i){
case i;
}
where i should int [sig][/sig]
 
Sure I do agree that the SWITCH statement accepts integer and it does accepts integer all the problem lies in the CASE.

If case statement is changed as
case 81:
case 82:
case 83:
case 84:
case 85:
System.out.println(&quot; grade A&quot;);

It check for the values 81 to 85, and than display Grade A.
Question is how can we give range of values in Switch statement i.e. 81 to 100.
if I have to give individual case 81: 82: just imagine number of times typing. Purpose of using the switch in above is to simply the ease of typing else have to use if statement. Worth thinking about it to make it possible in JAVA.

It is possible by using If statement. Still wanted in Switch.

Your Opinion and answers please
[sig][/sig]
 
Hi!

:), Maybe I work with a wrong Java. (WinNT4Wks, Jdk1.1.8):

Try this example:

public class SwitchTest {
public static void main(String[] args) {
char ch='a';
switch (ch) {
case 'a': System.out.println(&quot;a&quot;); break;
case 'b': System.out.println(&quot;b&quot;); break;
default: System.out.println(&quot;c&quot;);
}
}
}

The result is: a

Bye, Otto.
[sig][/sig]
 
All you have to do is to convert ggrade from Integer to basic type int with Integer.intValue() method and the switch will work fine.
That's all.
luck!
perseu
 
This is What can be done in VB ?
Question is want the same thing in JAVA !!!!!

Dim Number
Select Case Number
Case 81 To 100
Msgbox &quot;Grade A&quot;
Case 61 to 80
msgbox &quot;Grade B&quot;
Case 41 to 60
msgbox &quot;Grade C&quot;
Case 21 to 40
msgbox &quot;Grade D&quot;
Case 1 to 20
msgbox &quot;Grade E&quot;
Case Else
Msgbox &quot;Not a Number&quot;
End Select
 
Why complicate life?
Use &quot;if ... else if ... else&quot; instead of switch and your problem is solved.
Cheers!
phirst.
 
Do not include VB ideas with java.
For such programs, instead of using case statements use if-else statements.
 
you CAN NOT (read: this CAN'T PHYSICALLY BE DONE IN JAVA, to the best of my knowledge concerning current Java implementation) provide one single case statement for multiple integers. One case statement to one integer, that's all. The Java switch/case statement altogether is pretty weak in my eyes, but that's the way it is. Oh well. <br><br>Liam Morley<br><A HREF="mailto:"></A><br><A HREF=" TARGET="_new"> the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top