473,385 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 11554
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

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

Similar topics

10
by: Myster Ious | last post by:
Polymorphism replaces switch statements, making the code more compact/readable/maintainable/OO whatever, fine! What I understand, that needs to be done at the programming level, is this: a...
9
by: cousaert | last post by:
Newsgroups: alt.comp.lang.learn.c-c++ Date: Fri, 27 Aug 2004 12:36:11 +0200 Lines: 36 User-Agent: KNode/0.7.6 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii...
6
by: deanfamily11 | last post by:
I've set up a case statement to have my program determine where on the Cartesian plane a point the user enters is located. I keep getting the C2051 error when I compile. Any help? #include...
10
by: clueless_google | last post by:
hello. i've been beating my head against a wall over this for too long. setting the variables 'z' or 'y' to differing numbers, the following 'if/else' code snippet works fine; however, the ...
3
by: pgraeve | last post by:
I am a convert from VB to C# so bear with me on this "conversion" question C# switch statement seems to be the closest relative to VB's Select Case. I used VB's Select Case statement liberally. ...
5
by: Frederick Dean | last post by:
Hi,guys! I'm reading Stephen Dewhurst's book "C++ Gotchas"£¬in gothca #7, I meet a weird case: bool Postorder::next() { switch (pc) case START: while (true) if (!child()) { pc = LEAF; return...
22
by: John | last post by:
Hi Folks, I'm experimenting a little with creating a custom CEdit control so that I can decide on what the user is allowed to type into the control. I started off only allowing floating point...
2
by: Miyagi | last post by:
I have a bunch of conditions in which to determine different types of questions to ask in a survey I am constructing. Just was wondering if anybody has used a two-dimensional switch statement. If...
13
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.