469,271 Members | 1,014 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

Orhpaned case in switch statement

3
Hey. This is my first post on this forum, and I'm just coming for a little help. I just started to teach myself Java yesterday, and i am having trouble writing a switch statement.

The error:

When I attempt to compile my program, I am told:
test.java:12 ';' expected
Switch(x)

test.java:14: orphaned case
case x>70

Regarding the first error, everything that I've read states that a ";" is not needed after a switch declaration. I'm not sure why the compiler would be calling for this.

I really have no idea what's up the the orphaned case error. Other threads and websites and suggested faulty bracket placement, but I have already checked and re-written every bracket. Any ideas.

Thank you.


[code]
public class test
{
public static void main(String args[])
{

System.out.println("\n Enter an integer");
Scanner s = new Scanner(System.in);
int x = s.nextInt();

Switch(x)
{

case x>70:
System.out.println("Exceeding speed limit");
break;
case 65<x<=70:
System.out.println("Getting close");
break;
case x==65:
System.out.println("Cruising");
break;
case x<65:
System.out.println("Slow");
}
}
}
[/CODE}
Jul 26 '07 #1
6 11092
JosAH
11,448 Expert 8TB
It's "switch", not "Switch". On top of that, you can't switch on a comparison
expression e.g. "x > 70".

kind regards,

Jos
Jul 26 '07 #2
Zoner
3
That worked perfectly. Thanks for the help.
Jul 26 '07 #3
r035198x
13,262 8TB
That worked perfectly. Thanks for the help.
I'm always slow I know but could you post what you finally got to work (in code tags)?
Jul 26 '07 #4
Zoner
3
I'm always slow I know but could you post what you finally got to work (in code tags)?
Sorry, but i don't have the code anymore.

Essentially, I changed the uppercase "S" in Switch to a lower case one. Then, since boolean is not allowed in switches, I had to settle with an integer. I changed the values after "case" from, for example, x>70, to just 70.

Here is a very similar switch statement if this helps at all.

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3. public class PlanetWeight
  4. {    
  5.     public static void main(String args[])
  6.     {
  7.     System.out.println("Choose a planet\n");
  8.  
  9.     System.out.println("1.  voltar");
  10.     System.out.println("2.  Krypton");
  11.     System.out.println("3.  Fertos");
  12.     System.out.println("4.  Servontosn\n");
  13.  
  14.     Scanner a = new Scanner(System.in);
  15.     int planet = a.nextInt();
  16.  
  17.     System.out.println("Enter your weight");
  18.     Scanner s = new Scanner(System.in);
  19.     int x = s.nextInt();
  20.  
  21.     switch(planet) 
  22.     {
  23.  
  24.         case 1:
  25.             System.out.println("Your weight on Voltar is " + (0.091 * x));
  26.             break;
  27.         case 2:
  28.             System.out.println("Your weight on Voltar krypton is " + (0.720 * x));
  29.             break;
  30.         case 3:
  31.             System.out.println("Your weight on fertob is " + (0.865 * x));
  32.             break;
  33.         case 4:
  34.             System.out.println("Your weight on servontos is " + (4.612 * x));
  35.     }
  36.     }
  37. }
  38.  
Jul 26 '07 #5
r035198x
13,262 8TB
Sorry, but i don't have the code anymore.

Essentially, I changed the uppercase "S" in Switch to a lower case one. Then, since boolean is not allowed in switches, I had to settle with an integer. I changed the values after "case" from, for example, x>70, to just 70.

Here is a very similar switch statement if this helps at all.

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3. public class PlanetWeight
  4. {    
  5.     public static void main(String args[])
  6.     {
  7.     System.out.println("Choose a planet\n");
  8.  
  9.     System.out.println("1.  voltar");
  10.     System.out.println("2.  Krypton");
  11.     System.out.println("3.  Fertos");
  12.     System.out.println("4.  Servontosn\n");
  13.  
  14.     Scanner a = new Scanner(System.in);
  15.     int planet = a.nextInt();
  16.  
  17.     System.out.println("Enter your weight");
  18.     Scanner s = new Scanner(System.in);
  19.     int x = s.nextInt();
  20.  
  21.     switch(planet) 
  22.     {
  23.  
  24.         case 1:
  25.             System.out.println("Your weight on Voltar is " + (0.091 * x));
  26.             break;
  27.         case 2:
  28.             System.out.println("Your weight on Voltar krypton is " + (0.720 * x));
  29.             break;
  30.         case 3:
  31.             System.out.println("Your weight on fertob is " + (0.865 * x));
  32.             break;
  33.         case 4:
  34.             System.out.println("Your weight on servontos is " + (4.612 * x));
  35.     }
  36.     }
  37. }
  38.  
Oh, I see you are no longer matching against ranges anymore.
Jul 27 '07 #6
orlagh
1
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication18;

/**
*
* @author orlaghmoroney
*/

import java.util.Scanner;
public class JavaApplication18 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
//constant
final double annualFee= 3000;
final double greaterthanoreuqal90=1;
final double greaterthanorequal70= 0.75;
final double greatherthanorequal50=0.50;
final double lessthan50=0;
//required variables
String Name;
String studentNumber;
char firstLetter;
int softwaredevelopmentResult;
int mathsResult;
int learningtolearnResult;
int computerarchitectureResult;
double average;
int sum;
int userChoice;
double sum2;
double cashamountoffeesdue;

//Priming read, display menu & get user's initial choice
System.out.println("*Student finance/grants*");
System.out.println("1.\t Calculate Student Grants");
System.out.println("2.\t Current Overall Student Average");
System.out.println("3.\t Fee Data");
System.out.println("4.\t Exit");
System.out.println("****************************** ****");
userChoice=in.nextInt();

//loop until exit option chosen
while (userChoice != 4)
{
switch(userChoice){
case 1:



//Enter Student Name
System.out.print("Enter your Name ");
Name=in.nextLine();
in.nextLine();

System.out.print("Enter your Student Number");
studentNumber=in.nextLine();
firstLetter= studentNumber.charAt(0);



if(firstLetter != 'X'){
System.out.println("Re-enter Student Number");
}
else {
System.out.println("Enter Software Development Result");
softwaredevelopmentResult=in.nextInt();
System.out.println("Enter Maths Result");
mathsResult=in.nextInt();
System.out.println("Enter Learing To Learn Result");
learningtolearnResult=in.nextInt();
System.out.println("Enter Computer Architecture Result");
computerarchitectureResult=in.nextInt();

sum=softwaredevelopmentResult + mathsResult + learningtolearnResult + computerarchitectureResult;
average= sum / 4;
//Determine Grant Allocated
if (average >= 90){
System.out.println("100% of Fees Payed");
sum2=annualFee* greaterthanoreuqal90;
cashamountoffeesdue= annualFee-sum2;
System.out.printf("Fee Amount Due Is:%2f\n",cashamountoffeesdue);


}else
if (average>=70 && average <90){
System.out.println("75% of Fees Payed");
sum2=annualFee*greaterthanorequal70 ;
cashamountoffeesdue= annualFee-sum2;
System.out.printf("Fee Amount Due Is:%2f\n",cashamountoffeesdue);


}else
if (average>=50 && average <70){
System.out.println("50% of Fees Payed");
sum2=annualFee*greatherthanorequal50 ;
cashamountoffeesdue= annualFee-sum2;
System.out.printf("Fee Amount Due Is:%2f\n",cashamountoffeesdue);

}else
if(average<50){
System.out.println("No Grant Given");
sum2=annualFee*lessthan50 ;
cashamountoffeesdue= annualFee-sum2;
System.out.printf("Fee Amount Due Is:€.%2f\n",cashamountoffeesdue);


//Output results
System.out.printf("Student Name Is:%n%s",Name);
in.nextLine();
System.out.printf("Student Number Is:%s%n",studentNumber);
System.out.printf("The Average Is: %2f",average);
break;



case 2:



{


}
}
}
}
}
}
}
}


anyone know what I'm doing wrong because it is telling me case 2 is an orphaned case
Dec 4 '16 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

10 posts views Thread by Myster Ious | last post: by
9 posts views Thread by cousaert | last post: by
6 posts views Thread by deanfamily11 | last post: by
10 posts views Thread by clueless_google | last post: by
3 posts views Thread by pgraeve | last post: by
5 posts views Thread by Frederick Dean | last post: by
22 posts views Thread by John | last post: by
2 posts views Thread by Miyagi | last post: by
13 posts views Thread by Satya | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.