Jane
October 10th, 1999, 03:57 PM
I have this program here that reads in strings and doubles. I need to return the doubles in 2 decimal places. What do I need to add to the Reader file to achieve this? I know it's in java.text, but I'm not sure of the right syntax. The quantity and total should not have decimal places. Should those be read in as integers and if so, how do I do that? Thanks!
import java.io.*;
import java.text.*;
public class Product
{
class Reader
{
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
public String readString()
{
String r=null;
while(r==null)
try
{
r=reader.readLine();
}catch(Exception e){
r=null;
System.out.print("again:");
}
return r;
}
public double readDouble()
{
Double r=null;
while(r==null)
try
{
r = new Double(readString());
}catch(Exception e){
System.out.println("Exception:"+e.toString());
r=null;
System.out.print("again:");
}
if(r!=null)
return r.doubleValue();
else
return -1;
}
}
Reader reader = new Reader();
public static void main(String[]args)
{
Product app = new Product();
app.go();
}
public void go()
{
Productinfo productinfo = new Productinfo();
productinfo.getData();
productinfo.displayData();
productinfo.displayTotal();
try{System.in.read();}catch(Exception e){};
}
public class Productinfo
{
private String name, idnumber;
private double price, total, quantity;
public void getData()
{
System.out.print("Name:");
name=reader.readString();
System.out.print("ID Number:");
idnumber=reader.readString();
System.out.print("Price:");
price=reader.readDouble();
System.out.print("Quanity:");
quantity=reader.readDouble();
}
public void displayData()
{
System.out.println("The product is " + name);
System.out.println("The ID Number is " + idnumber);
System.out.println("The price is " + price);
System.out.println("The quantity is " + quantity);
}
public void displayTotal()
{
double total=(price * quantity);
System.out.println("The total value is " + total);
}
}
}
import java.io.*;
import java.text.*;
public class Product
{
class Reader
{
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
public String readString()
{
String r=null;
while(r==null)
try
{
r=reader.readLine();
}catch(Exception e){
r=null;
System.out.print("again:");
}
return r;
}
public double readDouble()
{
Double r=null;
while(r==null)
try
{
r = new Double(readString());
}catch(Exception e){
System.out.println("Exception:"+e.toString());
r=null;
System.out.print("again:");
}
if(r!=null)
return r.doubleValue();
else
return -1;
}
}
Reader reader = new Reader();
public static void main(String[]args)
{
Product app = new Product();
app.go();
}
public void go()
{
Productinfo productinfo = new Productinfo();
productinfo.getData();
productinfo.displayData();
productinfo.displayTotal();
try{System.in.read();}catch(Exception e){};
}
public class Productinfo
{
private String name, idnumber;
private double price, total, quantity;
public void getData()
{
System.out.print("Name:");
name=reader.readString();
System.out.print("ID Number:");
idnumber=reader.readString();
System.out.print("Price:");
price=reader.readDouble();
System.out.print("Quanity:");
quantity=reader.readDouble();
}
public void displayData()
{
System.out.println("The product is " + name);
System.out.println("The ID Number is " + idnumber);
System.out.println("The price is " + price);
System.out.println("The quantity is " + quantity);
}
public void displayTotal()
{
double total=(price * quantity);
System.out.println("The total value is " + total);
}
}
}