Google Answers Logo
View Question
 
Q: Problem with my code ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Problem with my code
Category: Computers
Asked by: charbrad-ga
List Price: $5.00
Posted: 29 Nov 2003 11:21 PST
Expires: 29 Dec 2003 11:21 PST
Question ID: 281676
I have a problem with my code...I am trying to create a JFrame that
holds names and phone numbers in a file.  This works ok it is just my
output, I do not get the phone number I have entered.  For Example
when I enter 1234567899 as the phone number my output is
96000990209E-308.  I think is has something to down with the following
segment of my code:

public void actionPerformed(ActionEvent e1)
{
double phoneNums;

try
{
phoneNums = Double.parseDouble(num.getText());
ostream.writeUTF(name.getText());
ostream.writeUTF(num.getText());
ostream.writeDouble(phoneNums);
name.setText("");
num.setText("");
}
catch(NumberFormatException e2)
{
System.err.println("Invalid Phone Number");
}
catch(IOException e3)
{
System.err.println("Error writing to file");
System.exit(1);
}
}
}

Can someone please take a look at it and let me know.

Clarification of Question by charbrad-ga on 29 Nov 2003 11:24 PST
Sorry I forgot to mention.....This is Java.
Answer  
Subject: Re: Problem with my code
Answered By: secret901-ga on 29 Nov 2003 11:49 PST
Rated:5 out of 5 stars
 
Hi charbrad!

I think that using double is not a good idea for storing phone
numbers. While double can store numbers up to (2-2^52)*2^1023, it does
so at a loss of precision, or in your case, misinterpretation.  The
data type most suited for phone number storage would be long, since it
stores integers up to 2^63-1 (int only stores up to 2147483647).

All you have to do is change the following two lines:
double phoneNums; => long phoneNums;
and 
phoneNums = Double.parseDouble(num.getText()); => phoneNums =
Long.parseLong(num.getText());

and your phone numbers should be displayed correctly.

I hope this answered your question.  If something doesn't work or you
need clarification, please use the "Request For Clarification" feature
before rating this answer and I will be happy to assist you further.
secret901-ga

Search strategy:

site:java.sun.com double primitive
site:java.sun.com long primitive


Helpful links:

Java API: Class Long
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Long.html

Java API: Class Double
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Double.html

Clarification of Answer by secret901-ga on 29 Nov 2003 11:55 PST
Hi again charbrad:

The line:
ostream.writeDouble(phoneNums);
should also be edited to:
ostream.writeLong(phoneNums);

Just to make sure there is no confusion, the code sniplet you provided
should look like this after you edited it:

public void actionPerformed(ActionEvent e1)
{
long phoneNums;

try
{
phoneNums = Long.parseLong(num.getText());
ostream.writeUTF(name.getText());
ostream.writeUTF(num.getText());
ostream.writeLong(phoneNums);
name.setText("");
num.setText("");
}
catch(NumberFormatException e2)
{
System.err.println("Invalid Phone Number");
}
catch(IOException e3)
{
System.err.println("Error writing to file");
System.exit(1);
}
}
}

My apologies,
secret901-ga

Request for Answer Clarification by charbrad-ga on 29 Nov 2003 12:50 PST
I made the changes and I rec'd 868841444291894 when entering
1234567890 as the phone number.  I am posting my entire code below
thanks for your help!

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CreatePhoneList extends JFrame implements ActionListener
{
private JLabel phoneList = new JLabel("Friends' Phone List");
Font bigFont = new Font("Bookman Old Style", Font.BOLD, 28);
private JLabel prompt = new JLabel("Enter Your Friend's Information",
JLabel.CENTER);
Font smallFont = new Font("Bookman Old Style", Font.BOLD, 15);
private JTextField name = new JTextField(15);
private JTextField num = new JTextField(10);
private Container con = getContentPane();


private JLabel label1 = new JLabel("Name", JLabel.LEFT);
private JLabel label2 = new JLabel("Phone Number",JLabel.LEFT);
private JButton inputDataButton = new JButton("Input Data");

DataOutputStream ostream;

public CreatePhoneList()
{
super("Create Phone List");

try
{
ostream = new DataOutputStream(new FileOutputStream("phonelist.dat"));
}
catch(IOException e)
{
System.err.println("UNABLE TO OPEN FILE");
System.exit(1);
}
setSize(300,200);
con.setLayout(new FlowLayout());
phoneList.setFont(bigFont);
con.add(phoneList);
con.add(prompt);
prompt.setFont(smallFont);
con.add(label1);
con.add(name);
con.add(label2);
con.add(num);
con.add(inputDataButton);

inputDataButton.addActionListener(this);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e1)
{
long phoneNums;

try
{
phoneNums = Long.parseLong(num.getText());
ostream.writeUTF(name.getText());
ostream.writeUTF(num.getText());
ostream.writeLong(phoneNums);
name.setText("");
num.setText("");
}
catch(NumberFormatException e2)
{
System.err.println("Invalid Phone Number");
}
catch(IOException e3)
{
System.err.println("Error writing to file");
System.exit(1);
}
}
}

Clarification of Answer by secret901-ga on 29 Nov 2003 14:21 PST
Hi again charbrad!

I ran your program on my computer (I added the method main() to it)
and the output file seemed to have been written correctly.  How did
you read the output file?  Did you use another class to read the
output file?  Perhaps you should check to make sure that the phone
numbers have been parsed correctly as long instead of another data
structure.

Hope that resolves it.  Please let me know if you still encounter problems.

secret901-ga

Request for Answer Clarification by charbrad-ga on 29 Nov 2003 15:37 PST
I am using another program to run it and here is the snipit from that code:

public void actionPerformed(ActionEvent e1)
{
String theName;
long phoneNums;

try
{
theName = istream.readUTF();
phoneNums = istream.readLong();
name.setText(theName);
num.setText(String.valueOf(phoneNums));
}
catch(EOFException e2)
{
closeFile();
}
catch(IOException e3)
{
System.err.println("Error reading file");
System.exit(1);
}
}
public void closeFile()
{
try
{
istream.close();
System.exit(0);
}
catch(IOException e)
{
System.err.println("Error closing file");
System.exit(1);
}
}
}

Clarification of Answer by secret901-ga on 29 Nov 2003 17:18 PST
Hi again charbrad!

I figured out why your program was giving wrong numbers :-)

When you were writing the data to the file, you had one extra line:
ostream.writeUTF(num.getText());

This line is unnecessary because you are already writing the number as
a long.  You can, if you choose, decide to write the phone number as a
UTF (so that the user can enter numbers such as 123-456-7890), then
you can just remove all reference to the long.

I am sure this will resolve your problem.  If any problem persists,
please let me know :-)

secret901-ga

Request for Answer Clarification by charbrad-ga on 29 Nov 2003 18:33 PST
Yes it worked............thank you!!!  One final question..this may
sound like a stupid question...is the dat file supposed to overwrite
each time the program is executed????

Clarification of Answer by secret901-ga on 29 Nov 2003 19:38 PST
Hi again charbrad,

I'm glad it worked out for you.  The current way that you are
declaring the FileOutputStream makes it necessary to create a whole
new file every time the program is run.  If you prefer that the
program append to the file instead of creating a new file each time,
change this line:
ostream = new DataOutputStream(new FileOutputStream("phonelist.dat"));
to:
ostream = new DataOutputStream(new FileOutputStream("phonelist.dat", true));

and that's it! :)

If something still doesn't work, please let me know before rating this answer.

secret901-ga

Link:
http://java.sun.com/j2se/1.4.2/docs/api/java/io/FileOutputStream.html#FileOutputStream(java.io.File,%20boolean)

Request for Answer Clarification by charbrad-ga on 29 Nov 2003 19:44 PST
You are the BEST!!!!  One more question before I close this
issue.....why can't I enter the dashes between the phone number?

Clarification of Answer by secret901-ga on 29 Nov 2003 20:07 PST
Hi charbrad!

As I mentioned before, if you enter dashes, then the input would would
not be able to be converted to long before.  If you'd like to enter
dashes in your phone number input, you can just treat the phone number
as a string like the name (i.e. don't convert it into a long, write it
to file as a UTF, and read it as a UTF).  If the user enters a string
of words for the phone number, the input would still be accepted.

Regards,
secret901-ga

Request for Answer Clarification by charbrad-ga on 29 Nov 2003 20:24 PST
You were a big help!!!!  Sometimes you just need another set of
eyes......thanks for lending me yours.

Clarification of Answer by secret901-ga on 29 Nov 2003 20:26 PST
Thanks you for your kind words and the tip! 

secret901-ga
charbrad-ga rated this answer:5 out of 5 stars and gave an additional tip of: $5.00
What a Genius!!!!!  You are fantastic.....thanks so much for your help!!

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy