Using Try and Catch; IOException Error Help!!
Greeting to all:
I am pounding out my fears of Java in a Sunday morning trying to learn the language. I am getting an IOException error in the following code:
/*
* SalesTax.java
*
* Created on July 1, 2007, 8:42 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package purchasetotal;
import java.io.IOException;
/**
*
* @author Big Popa P
*/
public class SalesTax {
private double cost = 0.0; //ITEM COST
private double tax = 0.0; //SALES TAX VALUE
private double totalCost = 0.0; //TOTAL COST OF SALES ITEM
/** Creates a new instance of SalesTax */
public SalesTax() {
}
public void setCost()
{
try
{
System.out.print("Enter the cost of the item: ");
cost = System.in.read();
}
catch (IOException cost)
{
System.out.println("Error! Please enter a 0.0 type number");
cost = System.in.read(); // ERROR IS Error
// System.out.print(0.0);
}
}// END setCost()
public void calculateTotalCost()
{
tax = 0.07 * cost;
totalCost = cost + tax;
}
public void getTotalCost()
{
System.out.println("The total cost is : $" + totalCost);
}
}
***********************************
Error:
incompatible types
found: int
required: java.io.IOException
***********************************
I am using Netbean 5.5.1 and I am pretty much focusing in on how to
use the try/catch and how to use the System.in.read().
Thanks any help
/*
* SalesTax.java
*
* Created on July 1, 2007, 8:42 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
import java.io.IOException;
/**
*
* @author Big Popa P
*/
public class SalesTax {
private double cost = 0.0; //ITEM COST
private double tax = 0.0; //SALES TAX VALUE
private double totalCost = 0.0; //TOTAL COST OF SALES ITEM
/** Creates a new instance of SalesTax */
public SalesTax() { }
public void setCost() {
try {
System.out.print("Enter the cost of the item: ");
cost = System.in.read();
} catch (IOException cost) {
System.out.println("Error! Please enter a 0.0 type number");
// cost = System.in.read(); // whenever you try to read from an input stream, it must be caught. You're outside of your try block here.
setCost(); // <-- try this instead.
}
}// END setCost()
public void calculateTotalCost() {
tax = 0.07 * cost;
totalCost = cost + tax;
}
public void getTotalCost() {
System.out.println("The total cost is : $" + totalCost);
}
}
1) cost is a double; System.in.read isn't. It's not a numeric type in fact, and that is one of the error.
2) I wouldn't use System.in.read in the first place. Look up the Scanner class (I think that it's part of the util package), and read a good tutorial on this and on how to get numeric input. That will get you started.
Here's a basic tutorial on scanning:
http://java.sun.com/docs/books/tutorial/essential/io/scanfor.html
Here's the scanner API. Look up hasNextDouble and nextDouble:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html#hasNextDouble()
Message was edited by:
petes1234