d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Quick Question.
Add Reply New Topic New Poll
Member
Posts: 3,234
Joined: Feb 21 2008
Gold: 59.20
Jan 11 2015 06:12pm
I'm running into a problem with this application not restarting at the first step. I want the application to run through, give the division's payroll, and restart asking for the name of the division again.

Here is the code at the moment

Code

import java.util.Scanner; // Scanner

public class PayrollPart2
{

public static void main( String[] args )
{
Scanner input = new Scanner( System.in ); // Scanner

// variables
char name; // variable for division's name
int number1; // variable for number of employees in the given division
double number2; // average salary of the employees
double product; // total payroll for each division


System.out.print( "Please enter Division's name, when you are done type stop to exit: ");
String divisionName = input.nextLine();

while(!("stop".equals(divisionName)))
{{
// prompt for number of employee's
System.out.print( "Please enter the number of employees in the division: ");
number1 = input.nextInt();
while (number1 <= 0)
{
System.out.print("Please enter a positive number of employees in the division:"); // prompt
number1 = input.nextInt(); // input
}

// prompt for employee's salary
System.out.print("Enter average salary for the employees: " );
number2 = input.nextDouble();
while (number2 <= 0)
{
System.out.print("Please enter a positive number for the employees salary:"); // prompt
number2 = input.nextDouble(); // input
}
}
//multiply Number1 by Number2
product = number1 * number2;

//displays division and total division payroll
System.out.printf( "The division %s has a payroll of $%.2f\n" ,divisionName, product );
}
} //end method main
} // end class PayrollPart2
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Jan 11 2015 06:23pm
You never ask for the division name again, so it will just repear off the first inputted name.

Add this right after you print the division payroll:

Code
System.out.print( "Please enter Division's name, when you are done type stop to exit: ");
divisionName = input.nextLine();
Member
Posts: 3,234
Joined: Feb 21 2008
Gold: 59.20
Jan 11 2015 07:01pm
Quote (Minkomonster @ Jan 11 2015 04:23pm)
You never ask for the division name again, so it will just repear off the first inputted name.

Add this right after you print the division payroll:

Code
System.out.print( "Please enter Division's name, when you are done type stop to exit: ");
            divisionName = input.nextLine();


I don't want the program to just ask for it again. I want the program to restart itself and begin at the top of the code again after giving the division's payroll at the end. Basically, I want it to restart the loop, but it's starting at step 2 instead of step 1.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jan 11 2015 07:08pm
Quote (Bummer @ Jan 11 2015 08:01pm)
I don't want the program to just ask for it again. I want the program to restart itself and begin at the top of the code again after giving the division's payroll at the end. Basically, I want it to restart the loop, but it's starting at step 2 instead of step 1.


it's clearly not part of the loop, so the solution is to add it to the loop. which is what minko did. you are free to move it to the front of the loop if you want, but you'll need to adjust it slightly.

This post was edited by carteblanche on Jan 11 2015 07:09pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Jan 11 2015 07:17pm
Quote (Bummer @ Jan 11 2015 08:01pm)
I don't want the program to just ask for it again. I want the program to restart itself and begin at the top of the code again after giving the division's payroll at the end. Basically, I want it to restart the loop, but it's starting at step 2 instead of step 1.


uh.....ok, whatever that means. Look, you really need to work on your communication. Because, I am going to show you how to do exactly what you just said to me, and I guarentee it is not what you want.

This is how you make that code execute again...

Code
import java.util.Scanner; // Scanner

public class PayrollPart2
{

public static void main( String[] args )
{
Scanner input = new Scanner( System.in ); // Scanner

// variables
char name; // variable for division's name
int number1; // variable for number of employees in the given division
double number2; // average salary of the employees
double product; // total payroll for each division


System.out.print( "Please enter Division's name, when you are done type stop to exit: ");
String divisionName = input.nextLine();

while(!("stop".equals(divisionName)))
{
//your while loop, removed for brevity
}

//make "program restart itself"
main(args);

} //end method main
} // end class PayrollPart2



Enjoy your stack overflow.
Member
Posts: 3,234
Joined: Feb 21 2008
Gold: 59.20
Jan 11 2015 07:27pm
Quote (carteblanche @ Jan 11 2015 05:08pm)
it's clearly not part of the loop, so the solution is to add it to the loop. which is what minko did. you are free to move it to the front of the loop if you want, but you'll need to adjust it slightly.


I had explained myself wrong. I have tried re-explaining it in a better way below.

Quote (Minkomonster @ Jan 11 2015 05:17pm)
uh.....ok, whatever that means. Look, you really need to work on your communication. Because, I am going to show you how to do exactly what you just said to me, and I guarentee it is not what you want.

This is how you make that code execute again...

Enjoy your stack overflow.


I am sorry for the miscommunication. I didn't mean that I wanted it to restart the loop. I meant that I wanted it to restart the entire code in a way that it begins asking for the name of the division again.

Adding the piece of code in the place that you specified in your first reply ended up in a result of
Code
Please enter Division's name, when you are done type stop to exit: Please enter the number of employees in the division:


Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Jan 11 2015 08:18pm
Right. And this is because you do not fully understand how Scanner and nextInt works.

Scanner buffers input from the given stream, in this case System.in. When you use nextInt, it takes the next int off the top of the stream. Everything else is left in the buffer. When you enter from keyboard you typed "123Enter". That enter is transalted to a newline character "\n" So the buffer looks like "123\n" So if you do a nextInt, "\n" is left in the buffer, and the next time you ask it for a nextLine, it will take that instead.

To fix this, either always use nextLine and parse the string into your datatype, an integer in this case (Integer.parseInt(string)), or call nextLine() after every nextInt to clear the buffer of that newline character.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll