import java.util.Scanner;


public class CalculatingSales
{
    private static boolean Double;



    public static void main(String[] args)
    {

        Scanner input = new Scanner ( System.in );

        int ProductNumber;
        double RetailPrice;
        double total = 0;
        int Product1Count = 0;
        int Product2Count = 0;
        int Product3Count = 0;
        int Product4Count = 0;
        int Product5Count = 0;

        System.out.print (" Enter Product Number: ");
        ProductNumber = input.nextInt();

        Switch ( ProductNumber );
        {

            case 1:
                System.out.print("Product1 Retail Price is 2.98");
            break;
            case 2:
                System.out.print("Product2 Retail Price is 4.50 ");
            break;
            case 3:
                System.out.print("Product3 Retail Price is 9.98");
            break;
            case 4:
                System.out.print("Product4 Retail Price is 4.49");
            break;
            case 5:
                System.out.print("Product5 Retail Price is 6.87");
                default :
                    System.Out.print("We are sorry, we do not have this product");


        System.out.print("Enter Retail Price");

        RetailPrice = input.nextDouble();

        while ( RetailPrice != -1);
        {  
            total += RetailPrice;

        }

        System.out.print("Total Price of Products sold " + total);

        System.out.printf("%s\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n",
                "Number of items sold for each product:",
                "Product1:", Product1Count,
                "Product2:", Product2Count,
                "Product3:", Product3Count,
                "Product4:", Product4Count,
                "Product5:", Product5Count);

    }


    }



}

IT KEEPS TELLING ME THAT IT CAN'T FIND SYMPOLE.

PLZZ HELP ME

Dani AI

Generated

Quick summary of what actually broke in the posted code: Java is case‑sensitive and statement headers must not end with stray semicolons, so the original switch/while blocks were never structured the way the author intended. correctly spotted the switch/case syntax issues; found the stray semicolon that turns the while into an empty loop; and ’s advice to place the switch inside a sentinel loop and accumulate counts/totals is the right design direction.

A clear, minimal flow to implement the assignment (product number / quantity pairs, sentinel to stop):

  1. Keep a fixed price map (array or small switch) for products 1–5. Use double for money and int for counts.
  2. Prompt for product number (use -1 or 0 as the sentinel). Read it first.
  3. Loop while productNumber != sentinel:
    • If productNumber is 1..5: prompt for quantity, read it, add quantity * price to total, and add quantity to the appropriate product count.
    • Else: print “invalid product” and skip counting.
    • Prompt for the next product number and repeat.
  4. After the loop print the total (format with two decimals) and the five product counts.

Implementation tips and gotchas not covered elsewhere: remove the unused field private static boolean Double; (it’s confusing and unnecessary). Never put a semicolon after a while(...) or switch(...). Make sure the closing brace for the switch is placed before continuing the rest of the code. Use break; on each case to avoid fall‑through unless fall‑through is intentional. Validate the quantity (non‑negative) and handle InputMismatchException if a non‑numeric token may be entered. For readable output use System.out.printf("%.2f", total).

These fixes implement ’s suggested structure while eliminating the syntax mistakes and found; following the stepwise flow above will produce correct totals and item counts for the assignment described by .

Recommended Answers

All 9 Replies

I'm pretty sure switch must be lowercase 's'.
Also no semicolon after the parentheses after switch.
And on first glance it doesnt look like you closed the statement in the appropriate place. You should close your switch after the text of the default case.
One last thing, please use code tags to make the code more readable.

ok then whats wrong with my while statement

I don't know you tell me. :)
But seriously, tell me the error being reported by the compiler.
Also post your updated code.

that program is an application to this question : ( Please check it for me
and tell me if there is anything wrong)

An online retailer sells five products whose retail prices are as follows: Product 1, $2.98; product 2, $4.50; product 3, $9.98; product 4, $4.49; and product 5, $6.87. Write an application that reads a series of pairs of numbers as follows:
a) product number
b) quantity sold

Your program should use a switch statement to determine the retail price for each product. It should calculate and display the total retail value of all products sold. Use a sentinel-controlled loop to determine when the program should stop looping and display the final results.

You should really proofread your code before posting.
Look at the print statement for your defalt case.

System.out.print

Shouldn't be capital Out

oh i am sorry i got it and i got the switch statement, but can you help me in solving that question ?

ok then whats wrong with my while statement

Do not put a semi-colon at the end of your while statement.

Do not put a semi-colon at the end of your while statement.

Wow how did I miss that one. ;)

Ok this code runs without syntax errors but you still have some program errors.

import java.util.Scanner;


public class CalculatingSales
{
private static boolean Double;

public static void main(String[] args)
{

Scanner input = new Scanner ( System.in );

int ProductNumber;
double RetailPrice;
double total = 0;
int Product1Count = 0;
int Product2Count = 0;
int Product3Count = 0;
int Product4Count = 0;
int Product5Count = 0;

System.out.print (" Enter Product Number: ");
ProductNumber = input.nextInt();

switch ( ProductNumber )
{

case 1:
System.out.println("Product1 Retail Price is $2.98");
break;
case 2:
System.out.println("Product2 Retail Price is $4.50 ");
break;
case 3:
System.out.println("Product3 Retail Price is $9.98");
break;
case 4:
System.out.println("Product4 Retail Price is $4.49");
break;
case 5:
System.out.println("Product5 Retail Price is $6.87");
default :
System.out.println("We are sorry, we do not have this product");
}//You forgot this

System.out.print("Enter Retail Price: ");

RetailPrice = input.nextDouble();

while ( RetailPrice != -1)/*This is an infinite loop which needs to be changed; it will never be equal to -1 so it will never end instead it will increase forever*/
{
total += RetailPrice;

}

System.out.print("Total Price of Products sold " + total);

System.out.printf("%s\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n",
"Number of items sold for each product:",
"Product1:", Product1Count,
"Product2:", Product2Count,
"Product3:", Product3Count,
"Product4:", Product4Count,
"Product5:", Product5Count);

}


}

I would write this with a 'while' and a 'switch' inside the 'while' and I would make the 'switch' add up everything for me which will be displayed after the 'while' loop is finished.

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.