Results 1 to 16 of 16
Like Tree1Likes
  • 1 Post By JosAH

Thread: What kind of loop?

  1. #1
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default What kind of loop?

    If I want to ask a user for certain options and then add those options, how do I do that? I did an if..else statment but is not working too good. Here is the code:

    Java Code:
    if(skillLevel == 2 || skillLevel == 3)
    		
    System.out.println("Choose your insurance options:");
    option = keyboard.nextInt();
    if(option == 1)
      insurance = 32.50;
    else
    if(option == 2)
       insurance = 20.00;
    else
    if(option == 3)
       insurance = 10.00;
    else
    if(option == 1 && option == 2)
       insurance = 52.50;
    else
    if(option == 1 && option == 3)
       insurance = 42.50;
    else
    if(option == 2 && option == 3)
       insurance = 30.00;
    else
    insurance = 0;
    Last edited by gabrielpr12; 01-27-2012 at 09:47 PM. Reason: indenting

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,515
    Blog Entries
    7
    Rep Power
    20

    Default Re: What kind of loop?

    Your indentation makes my eyes bleed; please change that, Java is not Pascal.

    kind regards,

    Jos
    diamonddragon likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default Re: What kind of loop?

    any input?

  4. #4
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: What kind of loop?

    Do you want the entire piece of code to execute ONLY when skillLevel is equal to either 2 or 3? If so, it's wrong; the way you currently have it, only the System.out.println will run only in that condition; the rest will always run.

    If you want an if statement to apply to multiple lines of code and not just one, you must put {curly braces} around the code that you want the if statement to apply to.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: What kind of loop?

    Will this ever be true?

    Java Code:
    if(option == 1 && option == 3)

    This equivalent to saying :
    Java Code:
    if (sexOfPerson == Sex.MALE && sexOfPerson == Sex.FEMALE) {
       // hire them out as a circus freak
    }

  6. #6
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default Re: What kind of loop?

    Quote Originally Posted by DiamondSoul View Post
    Do you want the entire piece of code to execute ONLY when skillLevel is equal to either 2 or 3? If so, it's wrong; the way you currently have it, only the System.out.println will run only in that condition; the rest will always run.

    If you want an if statement to apply to multiple lines of code and not just one, you must put {curly braces} around the code that you want the if statement to apply to.
    Got that, but how do I keep asking for an option and adding the price of that option? And what if there is two or three options?

  7. #7
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default Re: What kind of loop?

    Quote Originally Posted by Fubarable View Post
    Will this ever be true?

    Java Code:
    if(option == 1 && option == 3)

    This equivalent to saying :
    Java Code:
    if (sexOfPerson == Sex.MALE && sexOfPerson == Sex.FEMALE) {
       // hire them out as a circus freak
    }
    So, how should I write it?

  8. #8
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default Re: What kind of loop?

    Should I implement a do..while loop while the user wants to add insurance?

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: What kind of loop?

    Quote Originally Posted by gabrielpr12 View Post
    Should I implement a do..while loop while the user wants to add insurance?
    I've no idea as you're not really told us what the problem is. You may wish to spend a few minutes and type out a paragraph or two describing your problem in greater detail. Please have a look at my link below on how to ask smart questions that will give you more helpful suggestions on how to make your questions easier to answer.

  10. #10
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default Re: What kind of loop?

    I am trying to create a program that asks the user for their skill level, which will determine the pay rate. Now, skill level 1 does not have option to choose insurance, but skill level 2 and 3 do. Skill Level 2 and 3 can choose up to three insurance options. I need to create a loop that keeps asking for adding insurance. Then skill level 3 can opt to choose retirement plan at 3% of their gross pay. At the end I need to display the hours worked, pay rate, regular pay, overtime pay, total pay, deductions and net pay. The above code was just the if..else statement dealing with the insurance options. Here is the complete code:

    Java Code:
    import java.util.*;
    import javax.swing.*;
    
    public class Pay3
    {
    	public static void main(String[] args)
    	{
    		int addInsurance = 2;
    		final double MEDICAL = 32.50;
    		final double DENTAL = 20.00;
    		final double DISABILITY = 10.00;
    		int choice;
    		choice = 0;
    		double retirement;
    		double insurance;
    		double totalPay;
    		insurance = 0;
    		double rate;
    		rate = 0;
    		int option;
    		double hoursWorked;
    		double regularPay;
    		double overtimePay;
    		double netPay;
    		int skillLevel;
    		final int FULL_WEEK = 40;
    		final double OT_RATE = 1.5;
    		Scanner keyboard = new Scanner(System.in);
    		
    		System.out.print("What is your skill level?");
    		skillLevel = keyboard.nextInt();
    		if(skillLevel == 1)
    			rate = 17.00;
    			else
    				if(skillLevel == 2)
    					rate = 20.00;
    				else
    					if(skillLevel == 3)
    						rate = 22.00;
    		else
    			System.out.println("Invalid skill level.");
    		
    		System.out.print("How many hours did you work this week?");
    		hoursWorked = keyboard.nextDouble();
    		if(hoursWorked > FULL_WEEK)
    		{
    			regularPay = FULL_WEEK * rate;
    			overtimePay = (hoursWorked - FULL_WEEK) * OT_RATE * rate;
    		}
    		else
    		{
    		
    			regularPay = hoursWorked * rate;
    			overtimePay = 0.0;
    		}
    		
    		
    		if(skillLevel != 1)
    		
    			{System.out.println("Choose your insurance options:");
    			option = keyboard.nextInt();
    			if(option == 1)
    				insurance = MEDICAL;
    				else
    					if(option == 2)
    					insurance += insurance + DENTAL;
    					else
    						if(option == 3)
    						insurance += insurance + DISABILITY;
    							else
    								if(option == 1 && option == 2)
    								insurance = 52.50;
    									else
    										if(option == 1 && option == 3)
    										insurance = 42.50;
    									else
    										if(option == 2 && option == 3)
    										insurance = 30.00;
    										else
    										insurance = 0;}
    										
    		if(skillLevel == 3)
    			{System.out.println("Would you like to participate in thge retirement plan?");
    			choice = keyboard.nextInt();
    			if(choice == 1)
    				retirement = 0.03;
    			else
    				retirement = 0;}
    			totalPay = regularPay + overtimePay;
    			netPay = totalPay - insurance;
    		System.out.println("Regular pay is " +
    			regularPay + "\nOvertime pay is " + overtimePay + "\nHours worked: " + hoursWorked +
    			"\nYour hourly rate is: " + rate + "\nTotal gross pay is: " + (regularPay + overtimePay) +
    			"\nYour total deductions are: " + insurance + "\nYour net pay is: " + netPay);
    	}
    	
    }

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: What kind of loop?

    Suggestions:
    • Your if/else blocks (and other portions of your code) are indented in a very misleading way suggesting that you have nested if statements when in fact you don't. You should learn to indent correctly as this will make your code much easier for you and others to read and debug.
      For example compare your code:
      Java Code:
                  if(option == 1)
                      insurance = MEDICAL;
                      else
                          if(option == 2)
                          insurance += insurance + DENTAL;
                          else
                              if(option == 3)
                              insurance += insurance + DISABILITY;
                                  else
                                      if(option == 1 && option == 2)
                                      insurance = 52.50;
                                          else
                                              if(option == 1 && option == 3)
                                              insurance = 42.50;
                                          else
                                              if(option == 2 && option == 3)
                                              insurance = 30.00;
                                              else
                                              insurance = 0;}
      with correct indentation:
      Java Code:
               if (option == 1)
                  insurance = MEDICAL;
               else if (option == 2)
                  insurance += insurance + DENTAL;
               else if (option == 3)
                  insurance += insurance + DISABILITY;
               else if (option == 1 && option == 2)
                  insurance = 52.50;
               else if (option == 1 && option == 3)
                  insurance = 42.50;
               else if (option == 2 && option == 3)
                  insurance = 30.00;
               else
                  insurance = 0;
    • Next, nest all code blocks in curly braces. This will make the blocks more clear and will reduce careless errors. So I'd change the above to:
      Java Code:
               if (option == 1) {
                  insurance = MEDICAL;
               }
               else if (option == 2) {
                  insurance += insurance + DENTAL;
               }
               else if (option == 3) {
                  insurance += insurance + DISABILITY;
               }
               else if (option == 1 && option == 2) {
                  insurance = 52.50;
               }
               else if (option == 1 && option == 3) {
                  insurance = 42.50;
               }
               else if (option == 2 && option == 3) {
                  insurance = 30.00;
               }
               else {
                  insurance = 0;
               }
    • Next display better prompts. For instance, you are requesting insurance "options" but you're not showing a list of what options are avaiable, so this prompt means nothing to the user. Also it means nothing to me or your instructor since we have no way of interpretting what options mean what.


    Also, again your code is in error and will need to be re-written since option can never equal 2 and 3 at the same time.
    Last edited by Fubarable; 01-28-2012 at 05:29 PM.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: What kind of loop?

    But to answer your direct question, yes a do-while loop could work well for getting correct input from the user of your program.

  13. #13
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default Re: What kind of loop?

    Ok, so I got most of it fixed. Now my only problem is the do..while loop is not working correctly, it is not looping. This is the updated code:

    Java Code:
    import java.util.*;
    import javax.swing.*;
    
    public class Pay3
    {
    	public static void main(String[] args)
    	{
    		int addInsurance = 2;
    		final double MEDICAL = 32.50;
    		final double DENTAL = 20.00;
    		final double DISABILITY = 10.00;
    		int choice;
    		choice = 0;
    		double retirement;
    		double insurance;
    		double totalPay;
    		double deductions;
    		insurance = 0;
    		double rate;
    		rate = 0;
    		int option;
    		double hoursWorked;
    		double regularPay;
    		double overtimePay;
    		double netPay;
    		int skillLevel;
    		final int FULL_WEEK = 40;
    		final double OT_RATE = 1.5;
    		
    		Scanner keyboard = new Scanner(System.in);
    		
    		System.out.print("What is your skill level?");
    		skillLevel = keyboard.nextInt();
    		if(skillLevel == 1)
    			rate = 17.00;
    			else
    			if(skillLevel == 2)
    				rate = 20.00;
    			else
    			 if(skillLevel == 3)
    				rate = 22.00;
    		else
    			System.out.println("Invalid skill level.");
    		
    		System.out.print("How many hours did you work this week?");
    		hoursWorked = keyboard.nextDouble();
    		if(hoursWorked > FULL_WEEK)
    		{
    			regularPay = FULL_WEEK * rate;
    			overtimePay = (hoursWorked - FULL_WEEK) * OT_RATE * rate;
    		}
    		else
    		{
    		
    			regularPay = hoursWorked * rate;
    			overtimePay = 0.0;
    		}
    		
    		
    		totalPay = regularPay + overtimePay;
    if(skillLevel != 1)
    		
    			{
    			do
    			{
    				JOptionPane.showInputDialog("Choose your insurance options: \n1) Medical Insurance" +
    					"\n2) Dental Insurance \n3) Long-term Disability Insurance \n0) No Insurance");
    				option = keyboard.nextInt();
    				if(option == 1)
    				insurance = MEDICAL;
    				else
    					if(option == 2)
    					insurance += insurance + DENTAL;
    					else
    						if(option == 3)
    						insurance += insurance + DISABILITY;
    				}while(option != 0);
    				}
    else
    	insurance = 0;
    										
    		if(skillLevel == 3)
    			System.out.println("Would you like to participate in the retirement plan?");
    			choice = keyboard.nextInt();
    			if(choice == 1)
    				retirement = 0.03 * totalPay;
    			else
    				retirement = 0;
    			deductions = insurance + retirement;
    			netPay = totalPay - deductions;
    			
    		System.out.println("Regular pay is " +
    			regularPay + "\nOvertime pay is " + overtimePay + "\nHours worked: " + hoursWorked +
    			"\nYour hourly rate is: " + rate + "\nTotal gross pay is: " + (regularPay + overtimePay) +
    			"\nYour total deductions are: " + deductions + "\nYour net pay is: " + netPay);
    	}
    	
    }
    Last edited by gabrielpr12; 01-28-2012 at 06:23 PM.

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: What kind of loop?

    OK, let's look at the do-while loop a little more closely:

    Java Code:
             do {
                JOptionPane.showInputDialog("Choose your insurance options: \n1) Medical Insurance"
                            + "\n2) Dental Insurance \n3) Long-term Disability Insurance \n0) No Insurance");
                option = keyboard.nextInt();
                if (option == 1)
                   insurance = MEDICAL;
                else if (option == 2)
                   insurance += insurance + DENTAL;
                else if (option == 3)
                   insurance += insurance + DISABILITY;
             } while (choice != 0);
    This loops until choice is non-0. Where do you change choice inside of the loop? Should you perhaps be using another variable, not choice, as the control variable, the one that is checked in the while boolean condition?

  15. #15
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default Re: What kind of loop?

    Quote Originally Posted by Fubarable View Post
    OK, let's look at the do-while loop a little more closely:

    Java Code:
             do {
                JOptionPane.showInputDialog("Choose your insurance options: \n1) Medical Insurance"
                            + "\n2) Dental Insurance \n3) Long-term Disability Insurance \n0) No Insurance");
                option = keyboard.nextInt();
                if (option == 1)
                   insurance = MEDICAL;
                else if (option == 2)
                   insurance += insurance + DENTAL;
                else if (option == 3)
                   insurance += insurance + DISABILITY;
             } while (choice != 0);
    This loops until choice is non-0. Where do you change choice inside of the loop? Should you perhaps be using another variable, not choice, as the control variable, the one that is checked in the while boolean condition?
    Ok, I had the wrong word in there. If I put option != 0, it still not working.

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: What kind of loop?

    Quote Originally Posted by gabrielpr12 View Post
    Ok, I had the wrong word in there. If I put option != 0, it still not working.
    When this happens put in debug statements to test the code. For instance:

    Java Code:
    do {
       JOptionPane.showInputDialog("Choose your insurance options: \n1) Medical Insurance"
                   + "\n2) Dental Insurance \n3) Long-term Disability Insurance \n0) No Insurance");
       option = keyboard.nextInt();
       if (option == 1)
          insurance = MEDICAL;
       else if (option == 2)
          insurance += insurance + DENTAL;
       else if (option == 3)
          insurance += insurance + DISABILITY;
    
       // add the following
       System.out.println("Debug, option is: " + option);
    } while (option != 0);
    Then once you see this result, you should see how you can fix it.

Similar Threads

  1. what kind of application is this?
    By mymark in forum SWT / JFace
    Replies: 0
    Last Post: 08-03-2011, 12:11 AM
  2. Kind of stuck
    By Nicky Swans in forum New To Java
    Replies: 8
    Last Post: 10-22-2010, 02:46 PM
  3. ';' expected, not of the common kind..
    By Addez in forum New To Java
    Replies: 6
    Last Post: 09-02-2009, 04:37 AM
  4. how client know what kind of server
    By lemur in forum Networking
    Replies: 3
    Last Post: 05-31-2008, 07:11 AM
  5. Total Newbie, Be Kind :)
    By dazza-s in forum New To Java
    Replies: 11
    Last Post: 04-26-2008, 10:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •