While loops and IF constructs

Loop until option to stop and negative number input

Page 1 of 1

6 Replies - 6665 Views - Last Post: 24 October 2006 - 12:53 PM Rate Topic: -----

#1 panzer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 23-October 06

While loops and IF constructs

Post icon  Posted 23 October 2006 - 08:15 PM

// Week 2, Checkpoint: Payroll Program Part 1 
// Request payroll info and display a printout in monetary form.

import java.util.Scanner; //using class Scanner

public class Multiplication
{
   // main method begins execution of Java application
   public static void main( String args[] )
   {
	  // create Scanner to obtain input from command windows 
	  Scanner input = new Scanner( System.in );

	  double rate; // hourly rate
	  double hours; // hours worked
	  double pay; // sum of hourly times hours worked

	  // prompt for and input employee's name
	  System.out.println( "Please enter the employee's name or STOP to end the program: " );
	  String nameOfEmployee = input.nextLine(); //read a line of text
	  System.out.println();


	  
		 System.out.println( "Thank you for using the program...goodbye" );
	  System.out.println( "Enter the employee's hourly pay rate: "); // prompt
		 rate = input.nextInt(); // read first number from user
	  if ( rate > 0 )
		 System.out.println( "The rate must be a positve number, please enter another number: " );
	  System.out.println( "Enter the employee's hours worked: "); // prompt	  
		 hours = input.nextInt(); // read second number from user
	  if ( hours > 0 )
		 System.out.println( "The rate must be a positve number, please enter another number: " );	 

	  pay = hours * rate; // pay due
	  
	  System.out.print( nameOfEmployee );
	  System.out.printf( " is do $%,.2f\n", pay );


   } // end method main

} // end class Multiplication




Ok, I'm a beginner, can't stand coding but i understand after 10 years in the IT field and three years in management I need to start learning. Here is what I'm trying to do...an failing obviously.

I need to make the code continue to request employee information until the user enters stop as the employee name. The hourly rate and hours worked also need to be positive. The current code won't stop at STOP and allows negative numbers.

edit: modified title ~ jayman9

Is This A Good Question/Topic? 0
  • +

Replies To: While loops and IF constructs

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: While loops and IF constructs

Posted 23 October 2006 - 09:26 PM

You will want to consider using a WHILE loop to keep your program iterating until the condition has been entered that will stop the loop. You already have the code to handle negative values you just need to use {} to enlose the code in your IF statements to prevent certain bits of code from executing if the value is not positive.

An example would be:
while (nameOfEmployee.equalsIgnoreCase("STOP") != true)
{
   //your code here that gets addition user input
}



edit: modified to use equalsIgnoreCase
Was This Post Helpful? 1
  • +
  • -

#3 panzer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 23-October 06

Re: While loops and IF constructs

Posted 24 October 2006 - 04:07 AM

I will try this in about an hour. Thanks so much...
Was This Post Helpful? 0
  • +
  • -

#4 panzer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 23-October 06

Re: While loops and IF constructs

Posted 24 October 2006 - 11:46 AM


// Checkpoint: Payroll Program by  
// Request payroll info and display a printout in monetary form.

import java.util.Scanner; //using class Scanner

public class Multiplication
{  //1
   // main method begins execution of Java application
   public static void main( String args[] )
   {  //2
	  // create Scanner to obtain input from command windows 
	  Scanner input = new Scanner( System.in );

	  double rate; // hourly rate
	  double hours; // hours worked
	  double pay; // sum of hourly times hours worked
	  String nameOfEmployee; //



	  // prompt for and input employee's name
	  System.out.println( "Please enter the employee's name or STOP to end the program: " );
	  nameOfEmployee = input.nextLine(); //read a line of text
	  
	  while ( nameOfEmployee != "stop" ) //while (nameOfEmployee.compareToIgnoreCase("STOP") != 0)
   { //3
	  System.out.println( "Enter the employee's hourly pay rate: "); // prompt
		 rate = input.nextInt(); // read first number from user
	  
	  while ( rate < 0 )
   { //4begin while rate rule
	  System.out.println( "The rate must be a positve number, please enter another number: " );
		 System.out.println( "Enter the employee's hourly pay rate: "); // prompt
			rate = input.nextInt(); // read first number from user
   }  //4end while raterule


	  System.out.println( "Enter the employee's hours worked: "); // prompt   
		 hours = input.nextInt(); // read second number from user   

	  while ( hours < 0 )
   {  //5begin while hours rule
 
	  System.out.println( "The rate must be a positve number, please enter another number: " );	 
		 System.out.println( "Enter the employee's hours worked: "); // prompt   
			hours = input.nextInt(); // read second number from user
   }  //5 end while hours rule


	  pay = hours * rate; // pay due
	  
	  System.out.print( nameOfEmployee );
	  System.out.printf( " is do $%,.2f\n", pay );


	  // prompt for and input employee's name
	  System.out.println( "Please enter the employee's name or STOP to end the program: " );
		nameOfEmployee = input.nextLine(); //read a line of text

	  
	  } //3

		 System.out.println( "Thank you for using the program...goodbye" );

   } //2 end method main

} //1 end class Multiplication




Alright, I got the program to loop, but it ignores the stop command and at the end of the program, it doesn't wait for the user to input a new name, it simply asks for a new hourly rate.
Was This Post Helpful? 1
  • +
  • -

#5 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: While loops and IF constructs

Posted 24 October 2006 - 12:17 PM

Look at the method I am using in my previous post in the example WHILE loop. You need to use the equals or the equalsIgnoreCase or else one of the other methods availabe in order to compare two strings. These methods are found in the String API.
String API.
You cannot do a literal comparision using the != or == operators as Strings are objects.

Now I don't understand why you now have 3 WHILE loops when you only need one. The rest should have been left as IF statements, but you need to use { }'s in your IF statements to have them execute your code properly.

Here is an example of what I mean:
if ( rate > 0 )
{
   //code goes here if condition is true
}
if ( hours > 0 )
{
   //code goes here if condition is true
}

Was This Post Helpful? 1
  • +
  • -

#6 panzer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 23-October 06

Re: While loops and IF constructs

Post icon  Posted 24 October 2006 - 12:40 PM

I know you are trying and I really appreciate your help but the code for the while statement just does not work for me. I don't see what I am doing wrong at all.

View Postjayman9, on 24 Oct, 2006 - 12:17 PM, said:

Look at the method I am using in my previous post in the example WHILE loop. You need to use the equals or the equalsIgnoreCase or else one of the other methods availabe in order to compare two strings. These methods are found in the String API.
String API.
You cannot do a literal comparision using the != or == operators as Strings are objects.

Now I don't understand why you now have 3 WHILE loops when you only need one. The rest should have been left as IF statements, but you need to use { }'s in your IF statements to have them execute your code properly.

Here is an example of what I mean:
if ( rate > 0 )
{
   //code goes here if condition is true
}
if ( hours > 0 )
{
   //code goes here if condition is true
}

Was This Post Helpful? 0
  • +
  • -

#7 panzer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 23-October 06

Re: While loops and IF constructs

Posted 24 October 2006 - 12:53 PM

Got it, my last line was input.nextLine and that was causing the line for the username to not get read. I replaced that with input.next and it worked.

What is the difference between input.next and input.nextLine?


System.out.println( "Please enter the employee's name or STOP to end the program: " );
nameOfEmployee = input.next()[u]; //read a line of text
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1