Alright so... I haven't done ANY Java for an entire year so I've basically forgotten everything. This is an Intro Assignment that was given to us to just recap things... but I have a slight issue that I'm not sure how to fix at all. The situation is simple enough: ask questions and have the user enter data... However, when I use a do while loop, it seems to skip the first part. Let me get the code first:
Java: | import java.util.Scanner;
/** The "IntroAssignment" class.
* Enters the name, price, and quantity on hand for one or more products, as well
* as to output the product with the highest total value and the overall total value.
*/
public class IntroAssignment {
/**
* @param args
*/
public static void main (String[] args ) {
// Defining variables
Scanner keyboard = new Scanner (System. in);
String ans = " ";
Double total = 0. 00;
Double highestValue = 0. 00;
String highestProduct = "";
// Title
System. out. printf("%65s%n%n", "Inventory Management Program");
// Main Program, loops if the user wishes to enter another product
do
{
System. out. print ("Please enter the name of the next product: ");
String product = keyboard. nextLine();
System. out. printf("Please enter the unit price for %s: $", product );
Double unitPrice = keyboard. nextDouble();
System. out. printf("How many %s do you have on hand?: ", product );
int numOfProduct = keyboard. nextInt ();
Double totalCost = unitPrice*numOfProduct;
System. out. printf("You have %d %s @ $%.2f for a total value of $%.2f%n", numOfProduct, product, unitPrice, totalCost );
// Calculates the total value thus far and checks if this product has
// the highest value
total += totalCost;
if (totalCost > highestValue )
{
highestValue = totalCost;
highestProduct = product;
}
// Asks if the user wants to repeat, and loops if the answer is not y/n.
do
{
System. out. print("Do you have anymore products? (y/n): ");
ans = keyboard. next (). toUpperCase();
if (ans. charAt(0) == 'Y')
System. out. println();
}
while (ans. charAt(0) != 'Y' && ans. charAt(0) != 'N');
}
while (ans. charAt(0) == 'Y'); // repeats program if the user answers 'y'
// End of the program
System. out. println ();
System. out. printf ("The total value of all inventory is: $%.2f%n", total );
System. out. printf ("The item with the highest total value is $%.2f for %s.%n", highestValue, highestProduct );
System. out. println("Thank you for using the Inventory Management Program");
}
}
|
I've found that the problem is with code: | String product = keyboard.nextLine(); | . When the user enters Y in order to loop the program, it simply skips this piece of code and leaves the product name as just " ", and starts with askin how much is the unit price. I thought of using keyboard.next();, but not only would that not read everything if there was a space involved (i.e. "Large Items"), but it'd give me an error if I tried anything with a space. Sorry if my explanation isn't too clear. To better explain this, here's the output I get:
code: | Please enter the name of the next product: Item
Please enter the unit price for Item: $4
How many Item do you have on hand?: 4
You have 4 Item @ $4.00 for a total value of $16.00
Do you have anymore products? (y/n): y
Please enter the name of the next product: Please enter the unit price for : $ |
If anyone could help me out, it would be great. Thanks. |