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}
6 11554
It's "switch", not "Switch". On top of that, you can't switch on a comparison
expression e.g. "x > 70".
kind regards,
Jos
That worked perfectly. Thanks for the help.
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)?
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. -
import java.io.*;
-
import java.util.*;
-
public class PlanetWeight
-
{
-
public static void main(String args[])
-
{
-
System.out.println("Choose a planet\n");
-
-
System.out.println("1. voltar");
-
System.out.println("2. Krypton");
-
System.out.println("3. Fertos");
-
System.out.println("4. Servontosn\n");
-
-
Scanner a = new Scanner(System.in);
-
int planet = a.nextInt();
-
-
System.out.println("Enter your weight");
-
Scanner s = new Scanner(System.in);
-
int x = s.nextInt();
-
-
switch(planet)
-
{
-
-
case 1:
-
System.out.println("Your weight on Voltar is " + (0.091 * x));
-
break;
-
case 2:
-
System.out.println("Your weight on Voltar krypton is " + (0.720 * x));
-
break;
-
case 3:
-
System.out.println("Your weight on fertob is " + (0.865 * x));
-
break;
-
case 4:
-
System.out.println("Your weight on servontos is " + (4.612 * x));
-
}
-
}
-
}
-
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. -
import java.io.*;
-
import java.util.*;
-
public class PlanetWeight
-
{
-
public static void main(String args[])
-
{
-
System.out.println("Choose a planet\n");
-
-
System.out.println("1. voltar");
-
System.out.println("2. Krypton");
-
System.out.println("3. Fertos");
-
System.out.println("4. Servontosn\n");
-
-
Scanner a = new Scanner(System.in);
-
int planet = a.nextInt();
-
-
System.out.println("Enter your weight");
-
Scanner s = new Scanner(System.in);
-
int x = s.nextInt();
-
-
switch(planet)
-
{
-
-
case 1:
-
System.out.println("Your weight on Voltar is " + (0.091 * x));
-
break;
-
case 2:
-
System.out.println("Your weight on Voltar krypton is " + (0.720 * x));
-
break;
-
case 3:
-
System.out.println("Your weight on fertob is " + (0.865 * x));
-
break;
-
case 4:
-
System.out.println("Your weight on servontos is " + (4.612 * x));
-
}
-
}
-
}
-
Oh, I see you are no longer matching against ranges anymore.
/*
* 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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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 ...
|
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. ...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |