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);
	}
}
Re: Uni student in needs of help with java logical error 80 80

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

Re: Uni student in needs of help with java logical error 80 80

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.

Re: Uni student in needs of help with java logical error 80 80

You have to calculate the tax.

How to calculate sales tax

Re: Uni student in needs of help with java logical error 80 80

You have to calculate the tax.

How to calculate sales tax

Looked at a few examples and feel more confused now. lol I've been in the world of java for 2 weeks so thanks to all.

Re: Uni student in needs of help with java logical error 80 80

You have to calculate the tax.

How to calculate sales tax

Looked at a few examples and feel more confused now. lol I've been in the world of java for 2 weeks so thanks to all.

Re: Uni student in needs of help with java logical error 80 80
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?

commented: good help. +5
Re: Uni student in needs of help with java logical error 80 80
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

Be a part of the DaniWeb community

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