[5376] in java-interest
I need help....
daemon@ATHENA.MIT.EDU (Dhaval M Shah)
Wed Feb 7 04:09:37 1996
From: Dhaval M Shah <dhaval@cs.uow.edu.au>
To: java-interest-request@java.sun.com
Date: Wed, 7 Feb 1996 17:37:22 +1100 (EST)
Cc: java-interest@java.sun.com
Hello everybody,
I am a student learning java.
I have a problem with the following small code, particularly it does
not seem to print the value returned by the method compute_raise properly.
I am trying to print a double value using writeDouble(), I create an
instance of an outputstream under DataOutputStream, and use
outputstream.writeDouble() to print the double value. But the output I
get is a garbled and "junk"....
The full code and the output is also given.
Can somebody help me out ......
Code begins here .....
--------------------------------------------------------
import java.io.*;
class employee
{
String name;
String address;
long ssn;
private double salary;
double compute_raise(double percent)
{
return percent * salary;
}
employee(String new_name, String new_address, long new_ssn, double new_salary)
{
name = new_name;
address = new_address;
new_ssn = new_ssn;
salary = new_salary;
};
}
class mainclass
{
public static void main(String args[])
throws java.io.IOException
{
String new_name;
String new_address;
long new_ssn;
double new_salary;
double raise;
DataInputstream inputstream;
DataOutputStream outputstream;
inputstream = new DataInputStream(System.in);
outputstream = new DataOutputStream(System.out);
System.out.println("Enter the Name of the Employee : ");
new_name = inputstream.readLine();
System.out.println("Enter the Address : ");
new_address = inputstream.readLine();
new_ssn = 0;
System.out.println("Enter the Salary : ");
new_salary = inputstream.readDouble();
employee employeeobject = new employee(new_name, new_address, new_ssn, new_salary);
System.out.println("Raise = " + employeeobject.compute_raise(0.10));
raise = employeeobject.compute_raise(0.10);
System.out.print("\n");
outputstream.writeDouble(employeeobject.compute_raise(0.10)); // GARBLED O/P
System.out.print("\n");
outputstream.close();
}
}
--------------------------------------------------------
The resulting output after I run the above code is given below. I use
Sun Solaris and using the JDK-1_0 version of the java runtime for sun
-------------------------------------------------------------------
Output :
$java mainclass
Enter the Name of the Employee :
New Employee
Enter the Address :
New Address
Enter the Salary :
45000.00
Raise = 3.37548e-58
4sY@$sZ
$
------------------------------------------------------
Please notice the "junk" 4sY@$sZ instead of the value "4500". The value
does not seem to be garbled up - but the output. Why?
Can anybody help me out on that ....
thanks
dhaval
--
=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
Dhaval M. Shah
dhaval@cs.uow.edu.au (Home) : +61 42 258519
Dept. of Computer Science
University of Wollongong 2/13 Catherine St., Gwynneville,
NSW 2522 Australia NSW 2500 Australia
=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com