Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

problem with Double class, strings, and primate double type

  Asked By: Muaz    Date: Nov 30    Category: Java    Views: 1207
  

I tried to go into the chat room, but even though I have the most
recent VM from microsoft, the chat applet won't load. (sigh) I'm
not even sure there is anyone there.

Anyway.

I am taking a Java class, and am having trouble. I'm running a
student evaluation version of MS Visual J++ 6.0 and getting
a "Cannot convert 'Double' to 'double'" error with the code listed
below. BTW, using parseDouble instead of valueOf gets me a method
not found error. can anyone help me? Thanks in advance.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class CalcPay extends Applet implements ActionListener
{
Label hours = new Label("Enter Hours worked:");
Label rate = new Label("Enter Hourly Rate:");
TextField hoursWorked = new TextField("",10);
TextField ratePaid = new TextField("",10);
Font labelFont = new Font("Arial", Font.PLAIN,12);
Button Calc = new Button("Calculate");


public void init()
{
hours.setFont(labelFont);
rate.setFont(labelFont);
add(hours);
add(hoursWorked);
add(rate);
add(ratePaid);
add(Calc);
Calc.addActionListener(this);
ratePaid.addActionListener(this);
hoursWorked.requestFocus();
}

public void actionPerformed(ActionEvent thisEvent)
{
double dbl_hw, dbl_rp, pay;
String hw = hoursWorked.getText();
String rp = ratePaid.getText();
dbl_hw = Double.valueOf(hw);
dbl_rp = Double.valueOf(rp);
pay = dbl_hw * dbl_rp;
Label calcInfo = new Label("");
calcInfo.setText("Gross Pay is $" + pay);
add(calcInfo);
}
}

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Hooriya Khan     Answered On: Nov 30

I've already replied to this, but it didn't show up on the list. So
here goes again.

You get an error  because the Double.valueOf() method returns a
Double object (note the capital letter) and you're assigning it to a
double primitive. The correct method to use would be
parseDouble which you mentioned. It should be:

dbl_hw = Double.parseDouble(hw);
dbl_rp = Double.parseDouble(rp);

If that still gives you an error, it must be something to do with J++
, which I've never used

 




Tagged: