# Almost got it need something to stop program!

• 04-21-2005, 07:31 PM
Almost got it need something to stop program!
Hey there you all have been a super help. Heres my program. I have it all but how to stop if the user enters -99. I can't seem to figure out that part. Here is the assignment instructions:

Change this program to repeat until you enter a -99 for the quantity. Otherwise verifiy that the quantity is 1-100 inclusive and that the price is \$1.00 or more but less than \$100.00. If the entry is incorrect, keep repeating the request for that portion of the data until a correct value is entered. Display an error message if the entry is incorrect. Order of data being entered is up to you.

/**
* Loop exercise
* prompt for name of purchase item, quantiy, price,
* calculate the total including tax and display in one line
* continue until -99 error by user.
* @author (Donna Jones)
* @version (April 25th)
*/

import java.util.Scanner;
import java.text.NumberFormat;

public class editpurchase
{
//-----------------------------------------------------------------
// Calculates the total price of items includes quantity, item name
// cost per item, total cost including tax. Edits to tell user of incorrect
// input, printing error message and request re-entry
//-----------------------------------------------------------------
public static void main (String[] args)
{
final double TAX_RATE = 0.06; // 6% sales tax

String Item;
int quantity;
double subtotal,totalcost,unitPrice;

Scanner scan = new Scanner (System.in);

NumberFormat fmt1 = NumberFormat.getCurrencyInstance();
NumberFormat fmt2 = NumberFormat.getPercentInstance();

//PROMPTING FOR USER INPUT
do
{
System.out.print ("Enter the name of your item: ");
Item = scan.next();

do
{
System.out.print ("Enter the quantity: ");
quantity = scan.nextInt();

if (quantity < 0 || quantity >100) //less than zero & greater 100 print out following error message
{ System.out.println ("You've keyed an invalid quantity: Enter number between 1-100");
}
} while (quantity < 0 || quantity >100);

do
{
System.out.print ("Enter the unit price: ");
unitPrice = scan.nextDouble();

if (unitPrice < 1.00 || unitPrice > 99.99) //less than one dollar & 99.99 print out following error message
{ System.out.println ("You've keyed an invalid price: Enter a value between 1.00-99.99 ");
}
} while (unitPrice < 1.00 || unitPrice > 99.99);

System.out.println();

subtotal = quantity * unitPrice;
totalcost = (subtotal *TAX_RATE) + subtotal;

System.out.println ("Item purchased is: " + Item+ ". The quantity purchased was " +quantity
+ " the cost per item was: " +fmt1.format(unitPrice) + " for a subtotal of " + fmt1.format(subtotal)
+ " with a total cost of " + fmt1.format(totalcost));

System.out.println();

}
while (quantity !=-99);

}
}
• 04-22-2005, 04:48 AM
mikeBarr81
you need something like

Code:

```if(quantity == -99) {     return; }```
There are other ways you could do it too, but that is the shortest. It will return from the main() method if the quantity is -99
• 04-22-2005, 05:26 PM
That if (quantity ==-99)
return

asks for a while statement and does not seem to work

Does any one else have any ideas??
• 04-23-2005, 04:32 AM
mikeBarr81
What do you mean it asks for a while loop? What I posted has nothing to do with a while loop. Can you post exactly what you tried, and what the error message was?
• 04-23-2005, 09:41 AM
okay maybe I am confused as to where exactly I would put that If statement. I thought I would put it under the Quantity Do/while loop. Where would you recommend the statment you wrote goes?
• 04-23-2005, 04:47 PM
mikeBarr81
do
{
System.out.print ("Enter the quantity: ");
quantity = scan.nextInt();

if(quantity == -99)
{
return;
}

if (quantity < 0 || quantity >100) //less than zero & greater 100 print out following error message
{ System.out.println ("You've keyed an invalid quantity: Enter number between 1-100");
}
} while (quantity < 0 || quantity >100);