7 Replies - 603 Views - Last Post: 21 September 2011 - 09:17 PM Rate Topic: ***** 1 Votes

#1 b.netana  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 92
  • Joined: 11-September 11

stumped on proper use of while loop

Posted 21 September 2011 - 06:34 PM

okay here's my problem I have been trying to figure out why my code does not seem to work correctly. This is for an assignment that I have already worked on. I got a passing grade, but the program didn't work exactly as intended, and I didn't use the proper code (I was supposed to use just a while statement, and used do/while statement)

// Payroll2.java
// A program to calculate and print the department, name, and pay of an employee.
import java.util.Scanner; //program uses the Scanner class
import java.io.*;

public class Payroll2

{

   // main method begins execution of Java program.
   public static void main(String args[])
   {
       // create Scanner to obtain input from the command window
	   Scanner input = new Scanner (System.in);
	   
	   double number1; // first number to multiply
	   double number2; // second number to multiply
	   double product; // product of number1 and 
       
	   String name; // give the variable a name ( "name" in this case) 

	   System.out.print("Enter Department name: "); //prompt
	   name = input.nextLine(); // read name from user
	   
           do
	   {
	   System.out.print("Enter number of employees: "); // prompt
	   number1 = input.nextDouble(); // read first number from user
	   input.nextLine();
	   
	   System.out.print("Enter average salary: "); // prompt
	   number2 = input.nextDouble(); // read second number from user
	   input.nextLine();
	   
	   product = number1 * number2; // multiply numbers
	   
	   System.out.println("Department name:" + name); // display Department name
	   System.out.printf("Payroll is: $%.2f\n", product); // display product
	   }
	   
	   	   while ( !name.equals("stop") ); // sets the condition to end the program
	   
   } // end method main
   
} // end class Payroll2



The problem I had is that it would run one more instance of the program instead of just stopping when the user inputs stop in the Enter Department name prompt. Here is exactly what the teacher wanted:

1) Prompt the user for the department name.
2) Read in the department name.
3) While the department name is not equal to "stop" continue with the loop.
a) Prompt the user for the number of employees.
B) Read in the number of employees.
c) While the number of employees is negative, do the following:
i) Prompt the user for a positive number.
ii) Read in the value.
d) Prompt the user for the average salary.
e) Read in the average salary.
f) While the average salary is negative, do the following:
i) Prompt the user for a positive number.
ii) Read in the value.
g) Calculate the department payroll.
h) Display the department name and department payroll.
i) Prompt the user for the next department name.
j) Read in the department name.

I know that my code doesn't cover all of these criteria, and want to work on the other parts myself, but I'm just stumped on how to properly use the while loop here. Any help would be greatly appreciated. :bigsmile:

Is This A Good Question/Topic? 0
  • +

Replies To: stumped on proper use of while loop

#2 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: stumped on proper use of while loop

Posted 21 September 2011 - 06:50 PM

Um you at no point change the name. Notice you get the user input for name before you entered the do-while loop. The condition of the do-while loop is !name.equals("stop")

You could try moving
System.out.print("Enter Department name: "); //prompt
name = input.nextLine(); // read name from user



inside your do-while loop. You would still declare but also initialise the name variable outside of the loop to something such as String name = "default";

Your code would then loop something like
String name = "default";
do {
  System.out.print("Enter Department name: "); //prompt
  name = input.nextLine(); // read name from user
  
  System.out.println("Enter Number Of Employees");
  // etc...
while(!name.equals("stop"));

Was This Post Helpful? 0
  • +
  • -

#3 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: stumped on proper use of while loop

Posted 21 September 2011 - 06:55 PM

Put the while loop at the begining but first asign "" to name, you could do something like this:

name = "";

while(!name.equals("stop")){
   
      name = input.nextLine(); // read name from user

      if(!name.equals("stop")){
             //do the thing
      }
}



Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: stumped on proper use of while loop

Posted 21 September 2011 - 07:08 PM

rather then a big if() {followed by multiple statement}


while(true){
   
      String name = input.nextLine(); // read name from user

      if(name.equals("stop"))
          break;
}


Was This Post Helpful? 1
  • +
  • -

#5 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: stumped on proper use of while loop

Posted 21 September 2011 - 07:16 PM

View Postpbl, on 22 September 2011 - 02:08 AM, said:

rather then a big if() {followed by multiple statement}


while(true){
   
      String name = input.nextLine(); // read name from user

      if(name.equals("stop"))
          break;
}



thatīs the reason why you are a mentor :yes:
Was This Post Helpful? 0
  • +
  • -

#6 b.netana  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 92
  • Joined: 11-September 11

Re: stumped on proper use of while loop

Posted 21 September 2011 - 07:26 PM

hmm... I must not be doing this correctly it still seems to run one more instance when I enter "stop" as the department name. Here is how I've done it.
// Payroll2.java
// A program to calculate and print the department, name, and pay of an employee.
import java.util.Scanner; //program uses the Scanner class
import java.io.*;

public class Payroll2

{

   // main method begins execution of Java program.
   public static void main(String args[])
   {
       // create Scanner to obtain input from the command window
	   Scanner input = new Scanner (System.in);
	   
	   double number1; // first number to multiply
	   double number2; // second number to multiply
	   double product; // product of number1 and 
       
	   String name; // give the variable a name ( "name" in this case) 
       
	   name ="";
	   
	   while(!name.equals("stop")){
	    	   
	   System.out.print("Enter Department name: "); //prompt
	   name = input.nextLine(); // read name from user
	   
	   } // end while method
	   System.out.print("Enter number of employees: "); // prompt
	   number1 = input.nextDouble(); // read first number from user
	   input.nextLine();
	   
	   System.out.print("Enter average salary: "); // prompt
	   number2 = input.nextDouble(); // read second number from user
	   input.nextLine();
	   
	   product = number1 * number2; // multiply numbers
	   
	   System.out.println("Department name:" + name); // display Department name
	   System.out.printf("Payroll is: $%.2f\n", product); // display product

	   
   } // end method main
   
} // end class Payroll2



I'm not really sure why this isn't working as expected
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: stumped on proper use of while loop

Posted 21 September 2011 - 07:36 PM

// Payroll2.java
	   String name; // give the variable a name ( "name" in this case) 
       
	   name ="";
	   
	   while(!name.equals("stop")){
	    	   
	   System.out.print("Enter Department name: "); //prompt
	   name = input.nextLine(); // read name from user
	   
	   } // end while method


All your code prompting for the other fields should also be in the while
And if the name is "stop" how will you exit ?

Look at my previous post:


while(true){   // infinite loop
   
      String name = input.nextLine(); // read name from user

      if(name.equals("stop"))  // exit the loop
          break;

      ... input your other fields here

}




guido-granobles, on 21 September 2011 - 10:16 PM, said:

thatīs the reason why you are a mentor :)

Lol)))
Was This Post Helpful? 1
  • +
  • -

#8 b.netana  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 92
  • Joined: 11-September 11

Re: stumped on proper use of while loop

Posted 21 September 2011 - 09:17 PM

Thank you so much, pbl. I was just having a hard time there. Here is my newly completed code that covers all of the criteria of the assignment

// Payroll2.java
// A program to calculate and print the department, name, and pay of an employee.
import java.util.Scanner; //program uses the Scanner class
import java.io.*;

public class Payroll2

{

   // main method begins execution of Java program.
   public static void main(String args[])
   {
       // create Scanner to obtain input from the command window
	   Scanner input = new Scanner (System.in);
	   
	   double number1; // first number to multiply
	   double number2; // second number to multiply
	   double product; // product of number1 and 
       
	   
	   

while(true){   // infinite loop
	    

	   System.out.print("Enter Department name: "); //prompt
	   String name = input.nextLine(); // read name from user
	   
	    if(name.equals("stop"))  // exit the loop
	        break;

	   
	   System.out.print("Enter number of employees: "); // prompt
	   number1 = input.nextDouble(); // read first number from user
	   input.nextLine();
	      if( number1 <= -1){
		     System.out.print("Enter positive number of employees:"); // prompt
			 number1 = input.nextDouble();
			 input.nextLine();
		     }
	   
	   System.out.print("Enter average salary: "); // prompt
	   number2 = input.nextDouble(); // read second number from user
	   input.nextLine();
	   	      if( number2 <= -1){
		     System.out.print("Enter positive number for average salary:"); // prompt
			 number2 = input.nextDouble();
			 input.nextLine();
		     }
	   
	   
	   product = number1 * number2; // multiply numbers
	   
	   System.out.println("Department name:" + name); // display Department name
	   System.out.printf("Payroll is: $%.2f\n", product); // display product

	   } // end while method
	   
   } // end method main
   
} // end class Payroll2



Again Thank all of you very much. :bananaman:
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1