I am creating a program that takes in employee payroll information and then displays the average and total afterwards. Everything seems to be working correctly except for the fact I cannot get the program to end when I enter the sentinel value of -1 for the hours worked so that the program can display the total and average of the employees. Any help is greatly appreciated.
import java.util.Scanner;
public class PayrollDo
{
public static void main(String[] args)
{
int hoursWorked = 0;
int grossPay = 0;
int empCounter = 0;
int total = 0;
Scanner keyboard = new Scanner(System.in);
do {
total = total + grossPay; // add gross to total
empCounter = empCounter + 1; // incriment counter
System.out.print("Enter hours worked: ");
hoursWorked = keyboard.nextInt();
System.out.print("Enter hourly wage: ");
int hourlyWage = keyboard.nextInt();
//System.out.println("Grosspay is " + (hourlyWage * hoursWorked));
keyboard.nextLine();
System.out.println("Enter employee name ");
String name = keyboard.nextLine();
} while(hoursWorked != -1);
// if user entered at least one employee
if (empCounter != 0) {
// use number with decimal point to calculate average of employees
int average = (int) total / empCounter;
// display total and average (with two digits of precision)
System.out.printf("%nTotal of the %d employees entered is %d%n",
empCounter, total);
System.out.printf("Employee average is " + average);
}
else {
// no employees were entered, so output appropriate message
System.out.println("No employees were entered");
}
}
}
Test soon after entering the value whether it is -1 or not
System.out.print("Enter hours worked: ");
hoursWorked = keyboard.nextInt();
if (hoursWorked == -1) break;
edit
I think you will also have trouble with Integer division http://stackoverflow.com/questions/4685450/why-is-the-result-of-1-3-0
You could restructure your do loop to put the hoursWorked input last (otherwise you have to complete the entries) - and also, add a conditional to reject all entries that cycle, otherwise empCounter is off by one. And I think the total calculation needs reworking too:
do {
System.out.println("Enter employee name ");
String name = keyboard.nextLine();
System.out.print("Enter hourly wage: ");
int hourlyWage = keyboard.nextInt();
System.out.print("Enter hours worked: ");
hoursWorked = keyboard.nextInt();
keyboard.nextLine();
if (hoursWorked != -1) {
total = total + (hourlyWage * hoursWorked); //
empCounter = empCounter + 1; // incriment counter
}
} while(hoursWorked != -1);
In cases like this it can be simpler to have a question like "More Employees? (y/n)" that is used to terminate the loop, using a boolean flag as the while terminator.
Related
im using for loops to ask user to input item to buy and calculate the price...then it will ask whether the user will continue add another item...so its a loop again...then when the user stop the purchase...then it will calculate the totalprice...so im using:
totalprice+=total;
but when the outer for loops will repeat for the second customer...the value of totalprice is still the value of purchase from first customer so it will add up...is there anyway i can revert totalprice value to 0 everytime it loop for the second customer?
this is my code for that method
public static void makeOrder() {
for (index = 0; index < date.length; index++) {
double price = 0;
int order;
char addOrder;
String resume;
System.out.print("\nCustomer" + (index + 1));
System.out.print("\nEnter your name: ");
String name = input.nextLine();
System.out.print("Enter the date of reservation(DD/MM/YY): ");
date[index] = input.nextLine();
System.out.print("Enter your type of table(Couple/Family): ");
String table = input.nextLine();
do {
System.out.println("BERKAT RESTAURANT MENU:\n\n MEALS \n1-Beef Bolognese: RM17.00\n2-Chicken Marsala: RM 13.00\n3-Spaghetti Carbonara: RM 9.00\n4-Fillet Mignon: RM12.00");
System.out.println("\nDRINKS \n5-Strawberry Fruit Punch: RM6.00 \n6-Vanilla Smoothies: RM 7.00\n7-Sky Juice: RM 3.00");
System.out.print("\nEnter your choice of meals/drink: ");
order = input.nextInt();
System.out.print("Enter the quantity: ");
int quantity = input.nextInt();
System.out.print("Do you want to add order?(Y/N): ");
addOrder = input.next().charAt(0);
double total = calculatePrice(order, quantity);
subtotal += total;
} while (addOrder != 'N');
System.out.println("");
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
System.out.printf("The total price you have to pay is: RM%6.2f ", subtotal);
System.out.println("");
System.out.println("Thank you for coming to our restaurant, Please come again!");
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
}
}// makeOrder method
i want to revert the subtotal value back to 0.00 everytime the 'for' loops back
You want to make sure that you initialize subtotal to 0 at the start of your outside for loop. This way for each customer, it is always 0, regardless of the number of items added to the order.
public static void makeOrder() {
double subtotal = 0;
for(index = 0, index < date.length; index++) {
subtotal = 0;
/* your other code here */
}
}
You should initialize subtotal to 0 at the start of your for loop.
public static void makeOrder()
{
for(index=0;index<date.length;index++)
{
double subtotal=0;
double price=0;
int order;
char addOrder;
String resume;
System.out.print("\nCustomer"+(index+1));
System.out.print("\nEnter your name: ");
String name=input.nextLine();
System.out.print("Enter the date of reservation(DD/MM/YY): ");
date[index]=input.nextLine();
System.out.print("Enter your type of table(Couple/Family): ");
String table=input.nextLine();
do{
System.out.println("BERKAT RESTAURANT MENU:\n\n MEALS \n1-Beef Bolognese: RM17.00\n2-Chicken Marsala: RM 13.00\n3-Spaghetti Carbonara: RM 9.00\n4-Fillet Mignon: RM12.00");
System.out.println("\nDRINKS \n5-Strawberry Fruit Punch: RM6.00 \n6-Vanilla Smoothies: RM 7.00\n7-Sky Juice: RM 3.00");
System.out.print("\nEnter your choice of meals/drink: ");
order=input.nextInt();
System.out.print("Enter the quantity: ");
int quantity=input.nextInt();
System.out.print("Do you want to add order?(Y/N): ");
addOrder=input.next().charAt(0);
double total=calculatePrice(order,quantity);
subtotal+=total;
}while(addOrder!='N');
System.out.println("");
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
System.out.printf("The total price you have to pay is: RM%6.2f ",subtotal);
System.out.println("");
System.out.println("Thank you for coming to our restaurant, Please come again!");
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
}
}//makeOrder method
thank you guys for your help...so i declare subtotal=0.00 and it work like a charm
public static void makeOrder()
{
for(index=0;index<date.length;index++)
{
double price=0;
double subtotal=0.00;
int order;
char addOrder;
String resume;
System.out.print("\nCustomer"+(index+1));
System.out.print("\nEnter your name: ");
String name=input.nextLine();
System.out.print("Enter the date of reservation(DD/MM/YY): ");
date[index]=input.nextLine();
System.out.print("Enter your type of table(Couple/Family): ");
String table=input.nextLine();
do{
System.out.println("BERKAT RESTAURANT MENU:\n\n MEALS \n1-Beef Bolognese: RM17.00\n2-Chicken Marsala: RM 13.00\n3-Spaghetti Carbonara: RM 9.00\n4-Fillet Mignon: RM12.00");
System.out.println("\nDRINKS \n5-Strawberry Fruit Punch: RM6.00 \n6-Vanilla Smoothies: RM 7.00\n7-Sky Juice: RM 3.00");
System.out.print("\nEnter your choice of meals/drink: ");
order=input.nextInt();
System.out.print("Enter the quantity: ");
int quantity=input.nextInt();
System.out.print("Do you want to add order?(Y/N): ");
addOrder=input.next().charAt(0);
double total=calculatePrice(order,quantity);
subtotal+=total;
}while(addOrder!='N');
System.out.println("");
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
System.out.printf("The total price you have to pay is: RM%6.2f ",subtotal);
System.out.println("");
System.out.println("Thank you for coming to our restaurant, Please come again!");
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
}
}//makeOrder method
Im trying to write a code, that computes CD value, for every month.
Suppose you put 10,000 dollars into a CD with an annual percentage yield of 6,15%.
After one month the CD is worth:
10000 + 10000 * 6,15 / 1200 = 10051.25
After the next month :
10051.25 + 10051.25 * 6,15 / 1200 = 10102.76
Now I need to display all the results for the specific number of months entered by the user,
So
month1 =
month2 =
But whth this code I wrote, nothing is printed.
Can you see what's wrong?
Thanks in advance!
import java.util.Scanner;
public class CDValue {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter an amount");
double amount = input.nextInt();
System.out.println ("Enter the annual percentage yield");
double percentage = input.nextDouble();
System.out.println ("Enter the number of months");
int months = input.nextInt();
double worth = amount + amount * percentage / 1200;
for (int i = 1; i < months; i++) {
while (i != months) {
amount = worth;
worth = amount + amount * percentage / 1200;
}
System.out.print(worth);
You do not modify neither i nor months in
while (i != months) {
....
}
so if the (i != months) condition is satisfied, the loop runs forever, and you never get to System.out.print statement.
for (int i = 1; i < months; i++) {
while (i != months) {
//you have to modify i or to modify the while condition.
}
if you don't modify i in the while you can't exit from the loop
Corrected code-
import java.util.Scanner;
public class CDValue {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter an amount");
double amount = input.nextInt();
System.out.println ("Enter the annual percentage yield");
double percentage = input.nextDouble();
System.out.println ("Enter the number of months");
int months = input.nextInt();
double worth = amount + amount * percentage / 1200;
for (int i = 1; i <= months; i++)
{
System.out.print("Month " + i + " = " + worth);
amount = worth;
worth = amount + amount * percentage / 1200;
}
Note: If you want to print values for each month then the print statement should be inside the loop. You don't need two loops for the objective that you have mentioned above.
As you have been told your code won't get out of the while loop if you don't modify it. Simply remove the while loop. Your code should be like this:
import java.util.Scanner;
public class CDValue {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter an amount");
double amount = input.nextDouble();
System.out.println ("Enter the annual percentage yield");
double percentage = input.nextDouble();
System.out.println ("Enter the number of months");
int months = input.nextInt();
double worth = amount + amount * percentage / 1200;
for (int i = 1; i < months; i++) {
amount = worth;
worth = amount + amount * percentage / 1200;
}
System.out.print(worth);
}
}
Thanks! Solved it by using
{
System.out.print("Month " + i + " = " + worth);
amount = worth;
worth = amount + amount * percentage / 1200;
instead of while loop.
It works now :) Thanks so much!
This question already exists:
Scanner issue when using nextLine after nextXXX [duplicate]
Closed 7 years ago.
How am I able to get rid of one scanner? If I do use just one scanner the after the weekly_pay is output is:
Employee Name
Enter the hours worked for the week.
The program skips right over asking for the employee name variable. With both scanners it does indeed loop asking for the employee name as it should.
//Week 3 Assignment
package weeklypay2;
import java.util.Scanner; // importing the Java utility class scanner
public class WeeklyPay2 // class WeeklyPay
{
//main method
public static void main(String[] args)
{
Scanner Scanner = new Scanner(System.in);
Scanner Scanner1 = new Scanner(System.in);
String employee_name = null; // variable for employee name
double hours_worked = 0, // variable for weekly hours worked
pay_rate = 0, // variable for pay rate per hour
weekly_pay = 0; // weekly pay = hours * pay_rate
while (employee_name!="stop")
{
System.out.println("");
System.out.println("Employee Name"); // prompt, employees name
employee_name = Scanner1.nextLine();
if (employee_name.equalsIgnoreCase("stop"))
{
System.out.print("Exiting Program");
break;
} // ends if statement
else
{
System.out.println("Enter the hours worked for the week");
//prompt, hours worked for the current week
pay_rate = Scanner.nextDouble();
while(pay_rate < 0.01)
{
System.out.print("ERROR!!, Input a postive number: \n");
pay_rate = Scanner.nextDouble();
}
System.out.println("Enter the employees hourly pay rate");
// prompt, the employees pay rate
hours_worked = Scanner.nextDouble();
while(hours_worked < 0.01)
{
System.out.print("ERROR!!, Input a postive number: \n");
hours_worked = Scanner.nextDouble();
}
weekly_pay = (double) hours_worked * (double) pay_rate; // setting the variable weekly_pay
System.out.println(employee_name + "'s weekly pay is $" + weekly_pay ); // output the employees name and weekly pay
}
}
Scanner.close();
Scanner1.close();
} //ends main method
} //ends class WeeklyPay
Well the first thing I would say is be careful naming your scanner. Don't start that with a capital. Now a big thing is you don't want to compare strings using !=, this is not intended for string variables. There is a fun method for comparing strings that would be .equals() or .equalsIgnoreCase() depending if you want it non-case sensitive. Now after changing those few things in your code it works just fine. Also just a heads up Java also includes a method for formatting currency. You're pay numbers were missing a decimal. If you look at the NumberFormat I added after the scanner and then look at the println at the end you can see its very simple to use.
import java.text.NumberFormat;
import java.util.Scanner; // importing the Java utility class scanner
public class WeeklyPay2 // class WeeklyPay
{
//main method
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
NumberFormat format = NumberFormat.getCurrencyInstance();
String employee_name = "";
double hours_worked = 0, // variable for weekly hours worked
pay_rate = 0, // variable for pay rate per hour
weekly_pay = 0; // weekly pay = hours * pay_rate
while (!employee_name.equalsIgnoreCase("stop"))
{
System.out.println();
System.out.print("Employee Name: ");
employee_name = scanner.nextLine();
if (employee_name.equalsIgnoreCase("stop"))
{
System.out.print("Exiting Program");
break;
} // ends if statement
else
{
System.out.print("Enter the hours worked for the week: ");
//prompt, hours worked for the current week
pay_rate = scanner.nextDouble();
while(pay_rate < 0.01)
{
System.out.print("ERROR!!, Input a postive number: ");
pay_rate = scanner.nextDouble();
}
System.out.print("Enter the employees hourly pay rate: ");
// prompt, the employees pay rate
hours_worked = scanner.nextDouble();
while(hours_worked < 0.01)
{
System.out.print("ERROR!!, Input a postive number: \n");
hours_worked = scanner.nextDouble();
}
weekly_pay = (double) hours_worked * (double) pay_rate; // setting the variable weekly_pay
System.out.println(employee_name + "'s weekly pay is: " + format.format(weekly_pay)); // output the employees name and weekly pay
scanner.nextLine();
}
}
scanner.close();
} //ends main method
} //ends class WeeklyPay
And this is the output we would see:
Oh, PS: Keep the program cleaner by prompting for the input on the same line, more intuitive and better to look at. By all means add a line break after the input but not before :)
use Scanner1.nextLine() after getting the value for hours worked.
Refer modified code below:
import java.util.Scanner; // importing the Java utility class scanner
public class WeeklyPay2 // class WeeklyPay
{
//main method
public static void main(String[] args) {
Scanner Scanner1 = new Scanner(System.in);
String employee_name = null; // variable for employee name
double hours_worked = 0, // variable for weekly hours worked
pay_rate = 0, // variable for pay rate per hour
weekly_pay = 0; // weekly pay = hours * pay_rate
//Scanner1.useDelimiter("//n");
while (true) {
System.out.println("");
System.out.println("Employee Name"); // prompt, employees name
employee_name = Scanner1.nextLine();
if (employee_name.equalsIgnoreCase("stop"))
{
System.out.print("Exiting Program");
break;
}// ends if statement
else {
System.out.println("Enter the hours worked for the week");
//prompt, hours worked for the current week
pay_rate = Scanner1.nextDouble();
while (pay_rate < 0.01) {
System.out.print("ERROR!!, Input a postive number: \n");
pay_rate = Scanner1.nextDouble();
}
System.out.println("Enter the employees hourly pay rate");
// prompt, the employees pay rate
hours_worked = Scanner1.nextDouble();
while (hours_worked < 0.01) {
System.out.print("ERROR!!, Input a postive number: \n");
hours_worked = Scanner1.nextDouble();
//Scanner1.
}
Scanner1.nextLine();
weekly_pay = (double) hours_worked * (double)
pay_rate; // setting the variable weekly_pay
System.out.println(employee_name + "'s weekly pay is $" + weekly_pay); // output the employees name and weekly pay
}
}
Scanner1.close();
} //ends main method
}//ends class WeeklyPay
I have two programs:
Score.java set to do the following:
read scores from the keyboard and print their average.
The scores will be numeric and may include a decimal part.
For example a score might be 8.73 or some such. Different contests will have different numbers of judges. It will keep asking for and reading in scores until the user types 'done'. The program will then print the total score, the number of scores and the average score. The program will then prompt the user to see if there are any more contestants. If there are begin prompting for scores again. If there are no more then exit the program." I have it set to stop the program when you enter "N", and set to add future entries to the calculation after entering "Y".
import java.util.Scanner;
// This is the Score program
// Written by me
public class Score
{
public static void main(String args[])
{
Scanner game = new Scanner(System.in);
double num = 0.0;
double sum = 0.0;
int cnt = 0;
while (true)
{
System.out.println("Enter as many non-negative integers as you like ");
System.out.println("one at a time and I will find the average");
System.out.println("Enter done to stop entering numbers");
System.out.print("enter number: ");
String ans = game.next();
while (!ans.equals("done"))
{
num = Double.parseDouble(ans);
sum = sum + num;
cnt = cnt + 1;
System.out.print("enter number: ");
ans = game.next();
}
System.out.println(cnt);
System.out.println(sum);
System.out.println("Total Score " + sum + " count scores " + cnt + " avg score " + sum / cnt);
System.out.println("Enter another contestant (Y/N)?");
String str = game.next();
if (!str.equals("Y"))
break;
}
}
}
While the above process works, I cannot get my second program, Olympic.java, to work properly after typing "Y" to add more scores. Instead, it starts a whole new calculation of average instead of adding to the previous calculations:
import java.util.Scanner;
// This is the Olympic program
// Written by me
public class Olympic
{
public static void main(String args[])
{
Scanner game = new Scanner(System.in);
double num = 0.0;
double sum = 0.0;
int cnt = 0;
double highscore = Double.MAX_VALUE;
double lowscore = Double.MIN_VALUE;
while (true)
{
System.out.println("Enter as many non-negative integers as you like ");
System.out.println("one at a time and I will find the average");
System.out.println("Enter done to stop entering numbers");
System.out.print("enter number: ");
String ans = game.next();
lowscore = game.nextDouble();
highscore = game.nextDouble();
while (!ans.equals("done"))
{
num = Double.parseDouble(ans);
sum = (sum + num) - lowscore - highscore;
cnt = cnt + 1;
System.out.print("enter number: ");
if (num > highscore)
{
highscore = num;
}
if (num < lowscore)
{
lowscore = num;
}
ans = game.next();
}
System.out.println("Throwing out low score " + lowscore + " and high score " + highscore);
System.out.println("Total Score " + sum + " count scores " + cnt + " avg score " + sum / cnt);
System.out.println("Enter another contestant (Y/N)?");
String str = game.next();
if (!str.equals("Y"))
break;
}
}
}
So I did a really quick test
public static void main(String[] args) {
Scanner game = new Scanner(System.in);
while (true) {
System.out.println("Enter another contestant (Y/N)?");
String str = game.next();
if (!str.equalsIgnoreCase("Y")) {
break;
}
}
System.out.println("I'm free");
}
And this will exit fine.
As to your second problem. I think your logic is a little skewed. You could try something like...
Scanner game = new Scanner(System.in);
double num = 0;
double sum = 0;
int cnt = 0;
while (true) {
System.out.println("Enter as many non-negative integers as you like ");
System.out.println("one at a time and I will find the average");
System.out.println("Enter done to stop entering numbers");
double lowscore = Double.MAX_VALUE;
double highscore = 0;
System.out.print("enter number: ");
String ans = game.next();
while (!ans.equals("done")) {
num = Double.parseDouble(ans);
lowscore = Math.min(lowscore, num);
highscore = Math.max(highscore, num);
sum += num;
cnt++;
System.out.print("enter number: ");
if (num > highscore) {
highscore = num;
}
if (num < lowscore) {
lowscore = num;
}
ans = game.next();
}
sum -= lowscore;
sum -= highscore;
System.out.println("Throwing out low score " + lowscore + " and high score " + highscore);
System.out.println("Total Score " + sum + " count scores " + cnt + " avg score " + sum / cnt);
System.out.println("Enter another contestant (Y/N)?");
String str = game.next();
if (!str.equalsIgnoreCase("Y")) {
break;
}
}
This will output...
Enter as many non-negative integers as you like
one at a time and I will find the average
Enter done to stop entering numbers
enter number: 1
enter number: 2
enter number: 3
enter number: 4
enter number: 5
enter number: 6
enter number: 7
enter number: 8
enter number: 9
enter number: 10
enter number: done
Throwing out low score 1.0 and high score 10.0
Total Score 44.0 count scores 10 avg score 4.4
Enter another contestant (Y/N)?
y
Enter as many non-negative integers as you like
one at a time and I will find the average
Enter done to stop entering numbers
enter number: 1
enter number: 12
enter number: 13
enter number: 14
enter number: 15
enter number: 16
enter number: 17
enter number: 18
enter number: 19
enter number: 20
enter number: done
Throwing out low score 1.0 and high score 20.0
Total Score 168.0 count scores 20 avg score 8.4
Enter another contestant (Y/N)?
n
As to your exception. When using Scanner.nextDouble, it will throw an exception if the input is not parsable as a double. You will need to deal with this situation as you see fit...
When I enter input that satisfies everything and doesn't trigger any of my errors, the program just exits after last input like it is skipping over the for or if loop.
Also after System.out.printf("Enter the name of your second species: "); it won't allow for any input, it just skips to the next prompt. I'm not sure why that is. The section above it asking for the first species' info works fine.
import java.util.Scanner;
public class HW2johnson_pp1 {
public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);
System.out.printf("Please enter the species with the higher" +
" population first\n");
System.out.printf("Enter the name of your first species: ");
String Species1 = keyboard.nextLine();
System.out.printf("Enter the species' population: ");
int Pop1 = keyboard.nextInt();
System.out.printf("Enter the species' growth rate: ");
int Growth1 = keyboard.nextInt();
System.out.printf("Enter the name of your second species: ");
String Species2 = keyboard.nextLine();
System.out.printf("Enter the species' population: ");
int Pop2 = keyboard.nextInt();
System.out.printf("Enter the species' growth rate: ");
int Growth2 = keyboard.nextInt();
if (Pop2 > Pop1) {
System.out.printf("The first population must be higher. \n");
System.exit(0);
}
Species input1 = new Species();
input1.name = Species1;
input1.population = Pop1;
input1.growthRate = Growth1;
Species input2 = new Species();
input2.name = Species2;
input2.population = Pop2;
input2.growthRate = Growth2;
if ((input1.predictPopulation(1) - input2.predictPopulation(1)) <=
(input1.predictPopulation(2) - input2.predictPopulation(2))){
System.out.printf(Species2 + " will never out-populate " +
Species1 + "\n");
}
else {
for (int i = 0; input2.predictPopulation(i) <=
input1.predictPopulation(i); i++) {
if (input2.predictPopulation(i) == input1.predictPopulation(i)) {
System.out.printf(" will out-populate \n");
}
}
}
}
}
This for the predictPopulation():
public int predictPopulation(int years)
{
int result = 0;
double populationAmount = population;
int count = years;
while ((count > 0) && (populationAmount > 0))
{
populationAmount = (populationAmount +
(growthRate / 100) * populationAmount);
count--;
}
if (populationAmount > 0)
result = (int)populationAmount;
return result;
}
This is because you never print anything after Species 2 overtakes Species 1, except in the very special case that Species 2 and Species 1 have exactly the same population in some year.
This is because, when you enter Species 1's growth rate, you enter an integer, and then press Enter. keyboard.nextInt() swallows the integer, but leaves the newline on the input-buffer, so the subsequent keyboard.nextLine() thinks there's an empty line there waiting for it.