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 14 of 14

Thread: Switch Case Problem help

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Switch Case Problem help

    Hi I'm fairly new to java, so please don't laugh at me for posting a stupid question.

    As you can see on the code, I have created an Employee class, the constructors are set okay, and the print method is working fine. (disregard the calculate salary method)

    So I'm having a problem in the getPosition() method where I'm having troubles in displaying the contents of a switch case statement when I enter a char

    this is what the code looks like.

    class Employee{
     
    	String name;
    	long empNo;
    	int deptNo;
    	char empPosition;
    	int yearsOfExp;
    	float salary;
     
    	Employee(){				//default constructor
    		name = "";
    		empNo = 0;
    		deptNo = 0;
    		empPosition = 'E';
    		yearsOfExp = 0;
    		salary = 0;	
    	}
     
    	Employee(String n, long e, int d){
    		name = n;
    		empNo = e;
    		deptNo = d;
    		empPosition = 'E';
    		yearsOfExp = 0;
    		salary = 0;
    	}
     
    	Employee(String n, long e, int d, char em, int y){
    		name = n;
    		empNo = e;
    		deptNo = d;
    		empPosition = em;
    		yearsOfExp = y;
    		salary = 0;	
    	}
     
    	public char getPosition(char em){
     
    		switch(em){
    		case 'E':
    			System.out.print("Entry");
    			break;
    		case 'M':
    			System.out.print("Manager");
    			break;
    		case 'D':
    			System.out.print("Dirctor");
    			break;
    		case 'P':
    			System.out.print("Project Leader");
    			break;
    		default:
    			System.out.print("N/A");
    			//return em;
    		}
     
    	return em;
     
     
    	}
    	public void calculateSalary(){
     
    	}
    	public void print(){
    		System.out.println("Employee Name: " + name);
    		System.out.println("Employee Number: " + empNo);
    		System.out.println("Department Number: " + deptNo);
    		System.out.println("Position: " + empPosition);
    		System.out.println("Years of Experience: " + yearsOfExp);
    		System.out.println("Salary: " + salary);
    		System.out.println();
    	}
    }
     
    class EmployeeList{
    	public static void main(String[] args) {
     
    		Employee e1 = new Employee("John Doe",515542,10, 'M',2);
    		Employee e2 = new Employee("a",515542,10, 'M',2);
    		Employee e3 = new Employee("a",515542,10, 'M',2);
    		Employee e4 = new Employee("a",515542,10, 'M',2);
    		Employee e5 = new Employee("a",515542,10, 'M',2);
    		e1.print();
    		e2.print();
    		e3.print();
    		e4.print();
    		e5.print();
    	}
    }

    So when I try to run the program it should look like this

    Employee Name: John Doe
    Employee Number: 515542
    Department Number: 10
    Position: M
    Years of Experience: 2
    Salary: 0.0

    The "Position: M" must be "Position: Manager"

    How do I make that happen?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Switch Case Problem help

    What does the program output look like now?

    Why does the getPosition() method return its argument instead of a String?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Switch Case Problem help

    This is the program output:


    Employee Name: John Doe
    Employee Number: 515542
    Department Number: 10
    Position: M
    Years of Experience: 2
    Salary: 0.0

    Employee Name: a
    Employee Number: 515542
    Department Number: 10
    Position: M
    Years of Experience: 2
    Salary: 0.0

    Employee Name: a
    Employee Number: 515542
    Department Number: 10
    Position: M
    Years of Experience: 2
    Salary: 0.0

    Employee Name: a
    Employee Number: 515542
    Department Number: 10
    Position: M
    Years of Experience: 2
    Salary: 0.0

    Employee Name: a
    Employee Number: 515542
    Department Number: 10
    Position: M
    Years of Experience: 2
    Salary: 0.0

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Switch Case Problem help

    I don't see what the difference is between the "should look like" and the current output?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Switch Case Problem help

    oh ok, so this is the current output:

    Employee Name: John Doe
    Employee Number: 515542
    Department Number: 10
    Position: M
    Years of Experience: 2
    Salary: 0.0


    The new output that I want to look like should be like this:

    Employee Name: John Doe
    Employee Number: 515542
    Department Number: 10
    Position: Manager //This part is the one that should display, instead of an M
    Years of Experience: 2
    Salary: 0.0

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Switch Case Problem help

    Did you read the end of post #2?
    You should call that method to get the String you want.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Switch Case Problem help

    Quote Originally Posted by Norm View Post
    Did you read the end of post #2?
    Yes I do, how would I not return its argument?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Switch Case Problem help

    Why do you want to have the method return its argument?
    Why not have it convert its code letter into a String and return that String that you can print out where you want to see the String instead of the code letter.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Switch Case Problem help

    Quote Originally Posted by Norm View Post
    Why do you want to have the method return its argument?
    Why not have it convert its code letter into a String and return that String that you can print out where you want to see the String instead of the code letter.
    To be honest, I have no idea

    I'm still learning, so how do I exactly convert the code letter into a string?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Switch Case Problem help

    Use the case statements in getPosition to set a String for the char passed to it. Have getPosition return the String.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Switch Case Problem help

    Quote Originally Posted by Norm View Post
    Use the case statements in getPosition to set a String for the char passed to it. Have getPosition return the String.
    Sorry I don't really know what to do, can you show it to me visually?

    In my view, you're saying that it should look like this?

    	public String getPosition(char em){
     
    		switch(em){
    		case 'E':
    			System.out.print("Entry");
    			break;
    		case 'M':
    			System.out.print("Manager");
    			break;
    		case 'D':
    			System.out.print("Dirctor");
    			break;
    		case 'P':
    			System.out.print("Project Leader");
    			break;
    		default:
    			System.out.print("N/A");
    			//return em;
    		}
     
    	return getPosition;

    Wouldn't that cause an error?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Switch Case Problem help

    Define a String at the beginning of getPosition()
    In each case statement replace System.out.print with an assignment statement that sets the String to the value for the char.
    At the end of the getPosition() method return the String.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Switch Case Problem help

    Quote Originally Posted by Norm View Post
    Define a String at the beginning of getPosition()
    In each case statement replace System.out.print with an assignment statement that sets the String to the value for the char.
    At the end of the getPosition() method return the String.
    Ok I somehow got it, thanks for the help Norm!

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Switch Case Problem help

    Glad you got it.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Case Switch Help?
    By xionyus in forum What's Wrong With My Code?
    Replies: 17
    Last Post: October 19th, 2011, 08:59 PM
  2. Credit Card Problem (While and Switch will be used)
    By odun in forum Object Oriented Programming
    Replies: 3
    Last Post: March 29th, 2011, 12:54 AM
  3. Ranging Case Values in Switch Construct
    By WhiteSoup12 in forum Loops & Control Statements
    Replies: 1
    Last Post: February 6th, 2011, 11:50 AM
  4. CompareToIngnore case problem
    By newTaz in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 7th, 2010, 05:28 PM
  5. switch and math problem issues
    By emartino in forum Java Theory & Questions
    Replies: 1
    Last Post: January 19th, 2010, 05:34 PM