Just started my first unit in Java and my lecturer has set some code with logical errors in it. I think I have found them all bar one. the problem that I am having is that when I run this script it should calculate the Tax for the dollar amount but the tax stays at 55c whether it a $1 or $10 or even $100. It’s always 55c Tax.

//This program calculates the total price which includes sales tax

import java.util.Scanner;

public class SalesTax
{
	public static void main(String[] args)
	{
		//identifier declarations
		final double TAX_RATE =  0.55;
		double price;
		double tax;
		double total;
		String item;
		
		//create a Scanner object to read from the keyboard
		Scanner keyboard = new Scanner(System.in);

		//display prompts and get input
		System.out.print("Item description:  ");
		item = keyboard.nextLine();
		System.out.print("Item price:  $");
		price = keyboard.nextDouble();
		
		//calculations
		tax = TAX_RATE;
		total = price + tax;
		
		//display results
		System.out.print("item         $");
		System.out.println(price);
		System.out.print("Tax          $");
		System.out.println(tax);
		System.out.print("Total        $");
		System.out.println(total);
	}
}

Edited 6 Years Ago by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

yeah thats because you have set the tax as 55c,so it will remain as that only,how will its value change if you havent made any calculation to tax

yeah thats because you have set the tax as 55c,so it will remain as that only,how will its value change if you havent made any calculation to tax

Any ideas? If I have to change a heap of script I would say that I don’t need to do it for this exercise. The test is to look for logical errors. But For my sake though how would I do that.

total = price + tax;

use any calculator and try your code out on it.
so your code states if you have $5.00 + .55 = $5.55
$10.00 + .55 = $10.55.....think about it

if you had 5.5% tax, or .055
would you agree that it is price * 1.055?

Comments
good help.
total = price + tax;

use any calculator and try your code out on it.
so your code states if you have $5.00 + .55 = $5.55
$10.00 + .55 = $10.55.....think about it

if you had 5.5% tax, or .055
would you agree that it is price * 1.055?

Thanks heaps..... when its layed out it makes scence

This question has already been answered. Start a new discussion instead.