freezing with positive numbers

i need to create a looping program that when asked for # input and a

Page 1 of 1

8 Replies - 1151 Views - Last Post: 01 April 2008 - 11:15 AM Rate Topic: -----

#1 love4life   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 31-March 08

freezing with positive numbers

Post icon  Posted 31 March 2008 - 06:49 PM

package employee;
// Fig. 1.2 Employee.java
// Payroll program for Java class
import java.util.Scanner;

	public class Main
	{

	//main method
	public static void main ( String args[] )
	{
			 Scanner input = new Scanner(System.in);
								
				System.out.println( "Please enter employee name: " );
				String nameOfEmployee = input.next();
				if ( !nameOfEmployee.equals("stop"))
					
				{	
				double number1; // hourly rate, first number to multiply
				double number2; // hours worked, second number to multiply
				double product; // product of number1 and number2
				
				// create Scanner to obtain input from command window
				
				System.out.print("Please enter hourly rate: "); // prompt
				number1 = input.nextDouble(); // hourly rate
				if (number1>=0)
				number1 = input.nextDouble(); // hourly rate
					else	if (number1 < 0 );
					System.out.println( "Please enter a positive number: ");
					number1=input.nextDouble();
				
				System.out.print("Please enter amount of hours worked: "); // prompt
				number2 = input.nextDouble(); // hours worked
			   
				product = number1 * number2; // multiply numbers

				System.out.printf("Employee: %s\n", nameOfEmployee);
				System.out.printf("Total pay is $%.2f\n ", product); // display product 
									
			   
				} // end method main
				}
} // end class

 


Is This A Good Question/Topic? 0
  • +

Replies To: freezing with positive numbers

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: freezing with positive numbers

Posted 31 March 2008 - 07:58 PM

Gheeez teachers seam to have a lack of imagination all the assigments are the same

A full program doing exactly the same thing is available 1 or 2 topics below
Was This Post Helpful? 0
  • +
  • -

#3 love4life   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 31-March 08

Re: freezing with positive numbers

Posted 01 April 2008 - 07:05 AM

View Postpbl, on 31 Mar, 2008 - 07:58 PM, said:

Gheeez teachers seam to have a lack of imagination all the assigments are the same

A full program doing exactly the same thing is available 1 or 2 topics below


The teachers do lack imagination. Though sometimes they can be helpfully, rarely though. :-) Where is the program, whats the topic name or whatever its called?

My program runns but my if else's dont' seems to work and I dunno why not to mention I have no clue how to get it to loop nothing seems to work.
Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: freezing with positive numbers

Posted 01 April 2008 - 07:43 AM

Classes or no Classes? by raeNet
Was This Post Helpful? 0
  • +
  • -

#5 love4life   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 31-March 08

Re: freezing with positive numbers

Posted 01 April 2008 - 07:48 AM

That didn't really help. I need to make it so that you can not enter a negative number there wasn't anything in their code that had any equilvalance to it. Please help.
Was This Post Helpful? 0
  • +
  • -

#6 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: freezing with positive numbers

Posted 01 April 2008 - 08:12 AM

OK see what you mean

{
   
	 number1 = -1.0;
	 while(number1 < 0.0) {
		System.out.print("Enter hourly rate: ");
		number1 = input.nextDouble();
		if(number1 < 0.0)
		  System.out.println("Sorry must enter a positive number");
	 }


}



or if you do not want to duplicate the code

{
	   number1 = getAmount(input, "hourly rate");
	   number2 = getAmount(input, "workined hours");
}

static double getAmount(Scranner sc, String item) {
	double x = -1.0;
	while(x < 0.0) {
		System.out.print("Enter " + item + ": ");
		x = sc.nextDouble();
		if(x < 0.0)
		  System.out.println("Sorry number must be positive");
	}
	System.out.println();
	return x;
}


Was This Post Helpful? 0
  • +
  • -

#7 love4life   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 31-March 08

Re: freezing with positive numbers

Posted 01 April 2008 - 09:18 AM

ok if i do

	
number1 = -1.0;
	 while(number1 < 0.0) {
		System.out.print("Pleas enter your hourly rate: ");
		number1 = input.nextDouble();
		if(number1 < 0.0)
		  System.out.println("Sorry must enter a positive amount:" );
				number1 = input.nextDouble();



it freezes if you enter a positive number. but works fine if you enter a negative.

but if i enter
   
 number1 = -1.0;
	 while(number1 < 0.0) {
		System.out.print("Pleas enter your hourly rate: ");
		number1 = input.nextDouble();
		if(number1 < 0.0)
		  System.out.println("Sorry must enter a positive amount:" );
				

it doesn't let you put in a positive number if you entered a negative.

whats wrong?

This post has been edited by love4life: 01 April 2008 - 09:19 AM

Was This Post Helpful? 0
  • +
  • -

#8 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: freezing with positive numbers

Posted 01 April 2008 - 10:41 AM

You must have a } missing

   // init number1 to a negative value	
   number1 = -1.0;
   while(number1 < 0.0) {
	System.out.print("Pleas enter your hourly rate: ");
				// input a value in number1
	number1 = input.nextDouble();
				// if number just inputed is negative print an error message
	if(number1 < 0.0)
		System.out.println("Sorry must enter a positive amount:" );
	/////  remove that line :	 number1 = input.nextDouble();   
	}  // endloop while number1 < 0.0




This post has been edited by pbl: 01 April 2008 - 10:42 AM

Was This Post Helpful? 0
  • +
  • -

#9 love4life   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 31-March 08

Re: freezing with positive numbers

Posted 01 April 2008 - 11:15 AM

View Postpbl, on 1 Apr, 2008 - 10:41 AM, said:

You must have a } missing

   // init number1 to a negative value	
   number1 = -1.0;
   while(number1 < 0.0) {
	System.out.print("Pleas enter your hourly rate: ");
				// input a value in number1
	number1 = input.nextDouble();
				// if number just inputed is negative print an error message
	if(number1 < 0.0)
		System.out.println("Sorry must enter a positive amount:" );
	/////  remove that line :	 number1 = input.nextDouble();   
	}  // endloop while number1 < 0.0





That worked thank you
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1