0

Hey guys, I'm trying to create a program that has three items and adds the cost of the three items and prints the final total with sales tax. I need a subtotal, sales tax, and total amount. I'm having a problem getting the program to calculate the quantity correctly and run and I also am using the printf command so it can be rounded to two decimal places but i cant get it to work. Help anyone??

import java.util.*;

public class CashRegister
{


public static void main(String[] args)
{
//Declaring variables
double Item1, Item2, Item3;
double ItemPrice1, ItemPrice2, ItemPrice3;
double Subtotal, SalesTax, TotalAmount;

//Allowing keyboard input so I can print to the screen
Scanner keyboard = new Scanner(System.in);

System.out.println("Enter Quantity of first item: ");
Item1 = keyboard.nextDouble();

System.out.println("Enter the price of the first item: $ ");
ItemPrice1 = keyboard.nextDouble();

System.out.println("Enter Quantity of the second item: ");
Item2 = keyboard.nextDouble();

System.out.println("Enter the price of the second item: $ ");
ItemPrice2 = keyboard.nextDouble();

System.out.println("Enter Quantity of the third item: ");
Item3 = keyboard.nextDouble();

System.out.println("Enter the price of the third item: $ ");
ItemPrice3 = keyboard.nextDouble();

//Total of all three items before sales tax is added
Subtotal = ItemPrice1 + ItemPrice2 + ItemPrice3;

//Sales tax of 7% multiplied to the Subtotal
SalesTax = Subtotal * 0.07;

//Total amount which is the Subtotal plus the added 7% tax rate
TotalAmount = Subtotal + SalesTax;

System.out.printf("Subtotal: $%.2f \n"+ Subtotal);

System.out.printf("Sales Tax: $%.2f \n" + SalesTax);

System.out.printf("Total Amount: $%.2f \n" + TotalAmount);
4
Contributors
14
Replies
15
Views
7 Years
Discussion Span
Last Post by ncstplaya1234
0

The First problem is that you have not posted a program that will compile.
Can you fix that?

0

If you get your program to compile, it should work. There are a few things you need to be mindful of though:

1. Why are your quantity variables doubles? Can you buy 2.75 of a certain object?
2. You're using the Scanner method .nextDouble(). If this is just an exercise, that's OK, but it's still good practice to write foolproof code - what happens if the user types in an int, or a String?

0

Hey I got it to compile and it is correct but I need help to get two decimal places for the subtotal, salestax and total amount but i dont know how to do it, here are the given steps if you are willing to help me.

1. multiply the amount by 100
------------ Example: --- 1.7948 would become 179.48
2. add 0.5 to the result
------------ Example: --- 179.48 + 0.5 = 179.98
3. type cast the answer as an integer
------------ Example: --- 179.98 type cast as an integer gives 179
4. divide the resulting integer by 100 to put the two decimal places back where they should be
------------ Example: --- 179 becomes 1.79

It also says to use this statement to get the rounding:

System.out.printf("Subtotal: $%.2f \n", subTotal);

import java.util.*;

public class CashRegister 
{

	
	public static void main(String[] args) 
	{
		//Declaring variables 
		int item1, item2, item3;
		double itemPrice1, itemPrice2, itemPrice3;
		double subTotal, salesTax , totalAmount;
		
		
		//Allowing keyboard input so I can print to the screen
		Scanner keyboard = new Scanner(System.in);
		
		System.out.print("Enter Quantity of first item: ");
		item1 = keyboard.nextInt();
		
		System.out.print("Enter the price of the first item: $");
		itemPrice1 = keyboard.nextDouble();
		
		System.out.print("Enter Quantity of the second item: ");
		item2 = keyboard.nextInt();
		
		System.out.print("Enter the price of the second item: $");
		itemPrice2 = keyboard.nextDouble();
		
		System.out.print("Enter Quantity of the third item: ");
		item3 = keyboard.nextInt();
		
		System.out.print("Enter the price of the third item: $");
		itemPrice3 = keyboard.nextDouble();
		
		//Total of all three items before sales tax is added
		subTotal = itemPrice1 * item1 + itemPrice2 * item2 + itemPrice3 * item3;
				
		//Sales tax of 7% multiplied to the subtotal
		salesTax = subTotal * 0.07;
		
		//Total amount which is the subtotal plus the added 7% tax rate
		totalAmount = subTotal + salesTax;
		
		System.out.printf("Subtotal: $" + subTotal);
		
		System.out.printf("Sales Tax: $" + salesTax);
		
		System.out.printf("Total Amount: $" + totalAmount);
0

Oh yea I have use these numbers to make the result look like this with the exact numbers, i highlighted the sales tax because thats what those 4 steps were talking about

Enter the quantity of the first product: 3
Enter the price of the first product: $5.25
Enter the quantity of the second product: 2
Enter the price of the second product: $1.83
Enter the quantity of the third product: 7
Enter the price of the third product: $0.89

Subtotal: $25.64
Sales Tax: $1.79
Total Due: $27.43

0

But you just posted the steps you need to do, to solve that problem. I don't quite understand what you are asking.

0

Yes those are not my answers, those are the same numbers i need to use for my assignment and it needs to look exactly that way, i copyed and pasted from the assignment of what my result should look like with those numbers

0

For Example, what im saying is my answers look like 25.64353636,1.794838252, 27.34253636 not 25.64, 1.79, 27.43

0

Then just use the $%.2f\n...which casts to a float with 2 decimal places.

System.out.printf("Subtotal: $%.2f\n" + subTotal);
System.out.printf("Sales Tax: $%.2f \n" , salesTax);
System.out.printf("Total Amount: $%.2f \n" ,totalAmount);

Edited by Akill10: n/a

0

I know but im getting an error that looks like this

Subtotal: $Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '.2f'
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at CashRegister.main(CashRegister.java:46)

0

Getting an error though when i do it

Subtotal: $Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '.2f'
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at CashRegister.main(CashRegister.java:46)

0

I know but im getting an error that looks like this

Subtotal: $Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '.2f'
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at CashRegister.main(CashRegister.java:46)

0

Thats caused by using the wrong syntax with the print format. System.out.printf(); cannot be used the same way as print() or println().

printf (String format, Object... args)

Where the format is the $%.2f in your case. The '%' sign signals it is a specifier. The precision value 2 requires two places in the fraction, and the conversion symbol 'f' indicates a decimal representation of a floating-point number.

So, the thing you are doing wrong, is using a + in your print statement.

System.out.printf("Sales Tax:  $%.2f \n" [U],[/U] salesTax);

Edited by Akill10: n/a

0

Thats caused by using the wrong syntax with the print format. System.out.printf(); cannot be used the same way as print() or println().

printf (String format, Object... args)

Where the format is the $%.2f in your case. The '%' sign signals it is a specifier. The precision value 2 requires two places in the fraction, and the conversion symbol 'f' indicates a decimal representation of a floating-point number.

So, the thing you are doing wrong, is using a + in your print statement.

System.out.printf("Sales Tax:  $%.2f \n" [U],[/U] salesTax);

AAAAAAAAAhhhhhhhh omg im an idiot, thanks man I would have never seen that

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.