Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dear all, What am I doing wrong 1

Status
Not open for further replies.

rocknrisk

Programmer
May 14, 2002
43
GB
Dear all,

What am I doing wrong here. If I input, say, 1025 then I get 49 as my netCost. Why is the program changing my figure. This is driving me up the wall. I have the feeling, I'm making a very simple error, I just have no idea what it is

Thank you in advance and Merry Festive Season,
Clinton [rednose]

// ***************** Code ****************
import java.io.IOException;

public class NewYorkPlusTax {

public static void main(String[] args) {

double netCost;
double grossCost;
double taxAmount;

double TAX = 8.25; // this is a percentage

System.out.print("Enter Cost-Before-Tax of Item: ");
try {
netCost = (double)System.in.read();
// netCost = Double.valueOf(System.in.read()).doubleValue();
System.out.println("Item price = " + netCost);

taxAmount = netCost * TAX / 100;
System.out.println("Tax on Item = " + taxAmount);

grossCost = netCost + taxAmount;
System.out.println("Total Amount = " + grossCost);

} catch (IOException ioe) {
System.out.println("User Error");
}
}
}


"The important thing is not to stop questioning." - Albert Einstein
 
Try this:

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class test {

public static void main(String[] args) {

double netCost=0.0;


try {

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String temp = in.readLine();
try{

netCost = Double.parseDouble(temp);
}
catch(NumberFormatException nfe) {
System.out.println("Numbers only please, try again.");
}

System.out.println("Item price = " + netCost);

} catch (IOException ioe) {
System.out.println("User Error");
}
}
}

There could be a better way but this solution should work. ;-)

Cheers

frag patrick.metz@epost.de
 
Thank you. This is perfect.

Clinton "The important thing is not to stop questioning." - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top