How to write switch statement inside if statement?
My first problem is that I need to make a switch statement inside an 'if' statement. The second problem is in my code below.
Code :
import java.util.Scanner;
public class SalesCenter {
public SalesCenter() {
}
public static void main(String[] args) {
Associate Kirby = new Associate("Kirby", "Munsen",2);
Associate Bill = new Associate("Bill", "Donkin",3);
Associate King = new Associate("King", "Zeal",4);
Associate Luis = new Associate("Luis", "Sanfroth",5);
Manager John = new Manager();
System.out.print("Select Choice: Employee info(e) / Pay (p) / Quit (q)");
choice = input.nextLine();
System.out.print("Input employee number: ");
number = input.nextLine();
if (choice = e) {
switch (number) {
case 1: System.out.print(Manager John);break;
case 2: System.out.print(Associate Kirby);break;
case 3: System.out.print(Associate Bill);break;
case 4: System.out.print(Associate King);break;
case 5: System.out.print(Associate Luis);break;
}
}
}
}
The errors are here:
...\SalesCenter.java:29: ')' expected
case 1: System.out.print(Manager John);break;
^
...\SalesCenter.java:29: illegal start of expression
case 1: System.out.print(Manager John);break;
^
...\SalesCenter.java:30: ')' expected
case 2: System.out.print(Associate Kirby);break;
^
...\SalesCenter.java:30: illegal start of expression
case 2: System.out.print(Associate Kirby);break;
^
...\SalesCenter.java:31: ')' expected
case 3: System.out.print(Associate Bill);break;
^
...\SalesCenter.java:31: illegal start of expression
case 3: System.out.print(Associate Bill);break;
^
...\SalesCenter.java:32: ')' expected
case 4: System.out.print(Associate King);break;
^
...\SalesCenter.java:32: illegal start of expression
case 4: System.out.print(Associate King);break;
^
...\SalesCenter.java:33: ')' expected
case 5: System.out.print(Associate Luis);break;
^
...\SalesCenter.java:33: illegal start of expression
case 5: System.out.print(Associate Luis);break;
^
10 errors
Re: Help with Client Code
Hey Rezz,
Welcome to the Java Programming Forums :D
I can see a few errors in your code. Try this:
Code :
if (choice == "e") {
switch (number) {
case 1: System.out.print("Manager John");break;
case 2: System.out.print("Associate Kirby");break;
case 3: System.out.print("Associate Bill");break;
case 4: System.out.print("Associate King");break;
case 5: System.out.print("Associate Luis");break;
}
}
Firstly, you need to make sure you have the double equals in the IF statement.
Single equals (=) is assignment, double equals (==) is comparison.
Secondly, you need to make sure you have the String you want to print in System.out.print() inside quotes.
Give that a go and let us know if you have any more errors...
Re: Help with Client Code
I don't believe that the items are strings he is trying to output. I think he is trying to output the actual object. If that is so, you don't need the "manager" or "associate" part, but then you need to make sure that the objects are able to be displayed with System.out or you need to make a .toString() function for them and do it that way. I'm assuming there is more code to this because it is missing the declarations for choice and number. Also, you may have problems comparing choice to a character. If you do, instead of using the "==" use the .equalto() function. That fixes my problems every once and a while.
Re: Help with Client Code
Thanks for the replys already, but I'm still really struggling with this code. With this code I'm trying to return employee info or pay info depending on the choice ( e or p) after the user enters 'e' or 'p' they will be asked to enter an employee number which will then output the info wanted by the user. If the user wants to see pay they will then be asked to enter the amount of hours or weeks worked depending on the employee number, the Manager is payed in weeks and Associates are hourly. Below is the whole code.
Code :
import java.util.Scanner;
public class SalesCenter {
String choice;
int number;
public SalesCenter() {
}
public static void main(String[] args) {
Associate Kirby = new Associate("Kirby", "Munsen",2);
Associate Bill = new Associate("Bill", "Donkin",3);
Associate King = new Associate("King", "Zeal",4);
Associate Luis = new Associate("Luis", "Sanfroth",5);
Manager John = new Manager();
System.out.print("Select Choice: Employee info(e) / Pay (p) / Quit (q)");
choice = input.nextLine();
System.out.print("Input employee number: ");
number = input.nextInt();
if (choice == "e") {
switch (number) {
case 1: System.out.print(John);break;
case 2: System.out.print(Kirby);break;
case 3: System.out.print(Bill);break;
case 4: System.out.print(King);break;
case 5: System.out.print(Luis);break;
}
}
}
}
Re: Help with Client Code
Do you have an associate and manager class we can look at. That may help us figure out your problem a bit faster.
Re: Help with Client Code
Manager:
Code :
public class Manager extends Employee {
public Manager() {
super("John", "Smith", 55000, 1);
}
public double Pay( int weeks) {
double money;
money = super.GetSalary() / 52;
money = money*weeks;
return money;
}
}
Associate:
Code :
public class Associate extends Employee{
public Associate(String first, String last, int num) {
super(first, last, 18.75, num);
}
public double Pay(int hours) {
double money;
money = super.GetSalary() * hours;
return money;
}
}
Superclass Employee:
Code :
public class Employee {
public String Fname;
public String Lname;
private double salary;
private int Enumber;
public Employee () {
Fname = " ";
Lname = " ";
salary = 0;
Enumber = 0;
}
public Employee(String first, String last, double Salary, int num) {
Fname = first;
Lname = last;
salary = Salary;
Enumber = num;
}
public void SetSalary (double dollars) {
salary = dollars;
}
public double GetSalary() {
return salary;
}
public void SetEnumber (int num) {
Enumber = num;
}
public int GetEnumber() {
return Enumber;
}
}
Re: Help with Client Code
So i think i've got it all worked out.
Code :
import java.io.*;
public class SalesCenter {
private static BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in));
public SalesCenter() {
}
public static void main(String[] args) {
String choice = "e";
int number = 0;
Associate Kirby = new Associate("Kirby", "Munsen", 2);
Associate Bill = new Associate("Bill", "Donkin", 3);
Associate King = new Associate("King", "Zeal", 4);
Associate Luis = new Associate("Luis", "Sanfroth", 5);
Manager John = new Manager();
System.out
.print("Select Choice: Employee info(e) / Pay (p) / Quit (q)");
try {
choice = stdin.readLine();
System.out.print("Input employee number: ");
number = Integer.parseInt(stdin.readLine());
} catch (IOException e) {
e.printStackTrace();
}
if (choice.equals("e")) {
switch (number) {
case 1:
System.out.print(John.getInfo());
break;
case 2:
System.out.print(Kirby.getInfo());
break;
case 3:
System.out.print(Bill.getInfo());
break;
case 4:
System.out.print(King.getInfo());
break;
case 5:
System.out.print(Luis.getInfo());
break;
}
}
}
}
and i added this function to Employee
Code :
public String getInfo()
{
return Fname +"\n"+Lname+"\n"+salary+"\n"+Enumber;
}
}
This prolly isnt exactly what you looking for, but it works and should get you closer than you were a bit ago. Let me know if you have any more questions.