3 Replies - 341 Views - Last Post: 12 December 2010 - 10:09 AM Rate Topic: -----

#1 Justin3085  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 20-December 09

Loops

Posted 12 December 2010 - 09:04 AM

Ok this is a homework assignment, its been seen many times here for different problems. So here is mine now and the problems I'm having.
I have 2 classes, everything runs but when it prompts to enter an hourly rate or hours worked, unless it is a negative value it will continue the program.
Even when I enter a positive value it keeps prompting for entering a positive value. If you enter the same number twice it will move to the next block.

The next problem is once you've entered Employee name, hours, rate and it prints the employee name and the gross salary the program starts from the beginning but it skips over the "enter employee name" and goes to the next block.

I am assuming my {} may be wrong but I've tried moving some here and there and seem to do more damage than good. I've stared at this and others on DIC for the past few days, I've asked my instructor for assistance but all I get in return is to re-read the chapters for the week.

import java.util.Scanner;

public class payrollW4
{
	
	public static void main(String args[])
	{
		
		Scanner input = new Scanner(System.in);
		
		double Rate;
		int Hours;
		double product;
		boolean stop = false;
		while (!stop)
		{
		

		System.out.print("Please enter employee name or stop to quit the program.");
		String name = input.nextLine();
		System.out.println();//extra space
		
		if (name.equals("stop"))
		{
			System.out.println("Thank you for using Payroll Plus, Goodbye");
			stop = true;
		
		}//end if
		else
		{		
			
			System.out.print("Enter hourly rate: ");
			Rate = input.nextDouble();
			while (Rate <0);
			{
			System.out.println("Entry must be a positive value.");
			Rate = input.nextDouble();
			}
			
				System.out.print("Enter hours worked: ");
				Hours = input.nextInt();
				while (Hours < 0);
				{
				System.out.println("Please enter a postitive value.");
				Hours = input.nextInt();
			}				
			
			product = Rate * Hours;			
			System.out.println("Employee name" + name);
			System.out.println("Your gross pay is: $" + product);
			System.out.println();//extra space
			
			
		
		
			
			
					}//end while
				}//end else
		   }//end main
	   }//end class
	
	public class employeeinfo {

	
	public String empName = ""; 
    public double hrRate = 0;
    public int hrWorked = 0; 

		public employeeinfo(String name, double Rate, int Hours){//Constructor
		
			empName = name;
			hrRate = Rate;
			hrWorked = Hours;
	     
		}//end constructor
		
		public void setEmpName(String name){
			empName = name;
		}
		
		public void setHrRate(double Rate){
			hrRate = Rate;
		}
		
		public void setHrWorked(int Hours){
			hrWorked = Hours;
		}
		
		
		public String getName(){
			return empName;
		}
		
		public double getRate(){
			return hrRate;
		}
		
		public int getHrWorked(){
			return hrWorked;
		}
		
		public double getproduct(){
			double product = hrRate * (double)hrWorked;
			return product;
		}
		
		}//end class
		
		

	




Is This A Good Question/Topic? 0
  • +

Replies To: Loops

#2 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 982
  • Posts: 969
  • Joined: 30-September 10

Re: Loops

Posted 12 December 2010 - 09:38 AM

Hello there,

The first problem is happening because you have semi colons after some of your while loop conditions. An example being:


while (Rate <0); //do not need this semi colon!
{
    System.out.println("Entry must be a positive value.");
    Rate = input.nextDouble();
}





For the second problem, try using:

input.next();


instead of:

input.nextLine();


All your curly braces seem present and correct.

This post has been edited by CodingSup3rnatur@l-360: 12 December 2010 - 09:44 AM

Was This Post Helpful? 1
  • +
  • -

#3 I ♣ Seals  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 52
  • Joined: 20-November 10

Re: Loops

Posted 12 December 2010 - 10:05 AM

Regarding curly braces, it's conventional to put the opening one on the same line as the control statement associated with it, for example:
while (Rate <0) {
    System.out.println("Entry must be a positive value.");
    Rate = input.nextDouble();
}

That way, you'll be less tempted to put a semicolon where there shouldn't be one.
Was This Post Helpful? 1
  • +
  • -

#4 Justin3085  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 20-December 09

Re: Loops

Posted 12 December 2010 - 10:09 AM

Thank you very much for taking a look! Solved both problems perfectly. I will review semi colons and the .next and .nextLine usage.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1