Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: How to write switch statement inside if statement?

  1. #1
    Junior Member
    Join Date
    Jun 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    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
    Last edited by Rezz; June 10th, 2008 at 02:38 PM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Cool Re: Help with Client Code

    Hey Rezz,

    Welcome to the Java Programming Forums

    I can see a few errors in your code. Try this:

          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...
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default 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.

  4. #4
    Junior Member
    Join Date
    Jun 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    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;
            	}
            }
        }
     
     
     
     
        }

  5. #5
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default 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.

  6. #6
    Junior Member
    Join Date
    Jun 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Client Code

    Manager:
    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:

    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:

    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;
        }
     
    }

  7. #7
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Help with Client Code

    So i think i've got it all worked out.

    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

       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.

Similar Threads

  1. How to Write a simple XMPP (Jabber) client using the Smack API
    By JavaPF in forum Java Networking Tutorials
    Replies: 34
    Last Post: May 15th, 2013, 02:25 AM
  2. Java program to open jsp page on client side instead of server side
    By khanshakil in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: July 8th, 2009, 06:26 AM
  3. [SOLVED] web client
    By 5723 in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: June 10th, 2009, 04:44 PM
  4. Replies: 1
    Last Post: April 20th, 2009, 11:17 AM