I've been working on this project for the past couple of days. I can get the code to run, however, i'm suppose to be able to drop the java if I type -1 into the spot. I don't know how or where to place it. Here is the assignment and what I have so far:

1) Write a Java application that prompts the user for pairs of inputs of a product number (1-5), and
then an integer quantity of units sold (this is two separate prompts for input values). You must
use a switch statement and a sentinel-controlled loop (i.e. a loop that stops execution when an out
of range value, such as -1, is input). All 15 items below are for a single purchase. There are five
sets of inputs as follows:

Product 1 1 unit (cost is $2.98 per unit)
Product 2 2 units (cost is $4.50 per unit)
Product 3 3 units (cost is $9.98 per unit)
Product 4 4 units (cost is $4.49 per unit)
Product 5 5 units (cost is $6.87 per unit)

Your application must calculate and display the total retail value of all products sold, after all 5
pairs of inputs are completed. You must also display the total after each new pair of input values
is entered.

(This program was taken from Exercise 5.17 on page 228 of Deitel & Deitel's "Java How to
Program (Sixth Edition)" (2005 by Pearson Publishing Co.)

and here is the pseudocode we are offered as a potential guide:

6) Here is some pseudocode/skeleton Java code for one possible solution to the program to get you
started (this shows procedural code, but an object-oriented solution would have been better, since
Java is a pure object-oriented language):

import the classes you need
main
    declare productNo and quantity variables
    declare and initialize lineAmount and orderAmount variables
    set up a String for your output via the Scanner class (or you may use the JTextArea 
            GUI component – this will require additional research beyond the textbook!) 
start filling the String (or JTextArea) with the headers for Product, Quantity, Line 
        Cost, and Total Cost
prompt the user for the first productNo
while the productNo is not the sentinel value of -1
        get the quantity
        if the quantity is -1 then exit
        switch on productNo
                in each case, determine the new lineAmount
        add the lineAmount to the orderAmount
        add the new subtotal/order line information to the output String (or JTextArea)
        get the next productNo
output the total orderAmount

Here is what I have so far:

import java.util.Scanner;
public class Product
{
public static void main(String args[])
{
int cntr = 0;
int product = 0;
int units = 0;
double totalcost = 0;
Scanner MK = new Scanner(System.in);
cntr=0;
while (cntr >= 0 && cntr <=5)
{
System.out.println("Enter Product no.(1-5) or -1 to Quit");
product = MK.nextInt();
switch(product) {
case 1:
{
System.out.println("Product " + (cntr+1));
System.out.println("Enter Quantity or -1 to Quit");
product = MK.nextInt();
double cost = 2.98;
totalcost = totalcost + cost*product;
System.out.println("Current total cost: " + totalcost);
cntr++;
}
break;
case 2:
{
System.out.println("Product " + (cntr+1));
System.out.println("Enter Quantity or -1 to Quit");
product = MK.nextInt();
double cost = 4.50;
totalcost = totalcost + cost*product;
System.out.println("Current total cost: " + totalcost);
cntr++;
}
break;
case 3:
{
System.out.println("Product " + (cntr+1));
System.out.println("Enter Quantity or -1 to Quit");
product = MK.nextInt();
double cost = 9.98;
totalcost = totalcost + cost*product;
System.out.println("Current total cost: " + totalcost);
cntr++;
}
break;
case 4:
{
System.out.println("Product " + (cntr+1));
System.out.println("Enter Quantity or -1 to Quit");
product = MK.nextInt();
double cost = 4.49;
totalcost = totalcost + cost*product;
System.out.println("Current total cost: " + totalcost);
cntr++;
}
break;
case 5:
{
System.out.println("Product " + (cntr+1));
System.out.println("Enter Quantity or -1 to Quit");
product = MK.nextInt();
double cost = 6.87;
totalcost = totalcost + (cost*product);
System.out.println("Current total cost: " + totalcost);
cntr++;
}
}
System.out.println("Total cost-->" +totalcost);
}
}
}

any help will be awesome!
Thanks!

Your switch statement is brutally repetitive. I'm curious about what inspired you to do everything five times.

You can stop your loop with a break statement. But if you insist on doing that five times in the switch like you do everything else, then you will probably want to use a labelled break by giving your while a label. It's a feature that I have very rarely seen or used, but it looks like this:

    loop: while(true) {
        switch(0) {
        default: break loop; // break out of the loop instead of the switch
        }
    }

Otherwise a break inside the switch statement would only get you out of the switch statement.

Thank you! I was able to break it, however I also need to get it to run for all five products for a total price. The program will only run one time as it is with the loop while true added.

I think I got it. I changed the code to the following and it seems to be working.

case(-1):
break loop;

and it will still run the program like the class is asking but if i type -1 it breaks it and returns to C:\
Thank you for the help!

trying to run a program and get the following error
error
C:>javac extend.java
extend.java:4: error: class ExtGCD is public, should be declared in a file named
ExtGCD.java
public class ExtGCD
1 error

i think u must use this syntax: publis static void main(String[] args)throws IOException
CAUSE it will help compiler to ignore unnecessary errors

meenal9: that is the very WORST advice ever. allowing your main method to throw an exception might look smart, but if you worked for me, that would mean: bye-bye.
the main method is the very last place where you can actually catch the exceptions thrown and add some decent exception handling.

Oliver.mcKoy: not only do you hijack a thread, but that is by far the most simple error message to read and analyse.
your filename is extend. your filename should be ExtGCD.
we're not talking brain surgery here.

@meena19: sorry, that's really terrible advice.
Exceptions are there to help you. They trap errors in your program and create detailed message explaining what went wrong and where. There's no such thing as an "unnecessary error".
The worst possible thing to do is to ignore them. Unless you have a reason to do otherwize, catch your exceptions as early as possible, print any relevant local variables, and call the exception's printStackTrace() method to see all the helpful relevant info.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.