双重不能被取消引用? [英] Double cannot be dereferenced?

查看:201
本文介绍了双重不能被取消引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

String mins = minsField.getText(); 

    int Mins;
    try
    {
        Mins = Integer.parseInt(mins);

     }
     catch (NumberFormatException e)
     {
         Mins = 0;
     }

    double hours = Mins / 60;

    hours.setText(hoursminsfield);

问题是 Double无法解除引用
我如何解决这个问题????任何帮助将被贴上

The problem is that Double cannot be dereferenced. How can I fix this problem?
Any help will be appriciated

推荐答案

编辑4/23 / 12



double不能取消引用是一些Java编译器在尝试调用原语的方法时给出的错误。在我看来, double没有这样的方法会更有帮助,但我知道什么。

EDIT 4/23/12

double cannot be dereferenced is the error some Java compilers give when you try to call a method on a primitive. It seems to me double has no such method would be more helpful, but what do I know.

从你的代码,似乎您可以通过执行
小时将小时的文本表示复制到 hoursminfield 中。的setText(hoursminfield);
这有一些错误:
1)小时是一个 double 这是一个原始类型,没有方法可以调用它。这是什么给你你所问的错误。
2)你不说什么类型的小时空格,也许你还没有宣布。
3)通过使其成为方法的参数来设置变量的值是不寻常的。它有时会发生,但通常不是这样。

From your code, it seems you think you can copy a text representation of hours into hoursminfield by doing hours.setText(hoursminfield); This has a few errors: 1) hours is a double which is a primitive type, there are NO methods you can call on it. This is what gives you the error you asked about. 2) you don't say what type hoursminfield is, maybe you haven't even declared it yet. 3) it is unusual to set the value of a variable by having it be the argument to a method. It happens sometimes, but not usually.

执行你想要的代码行是:

The lines of code that do what you seem to want are:

String hoursrminfield; // you better declare any variable you are using

// wrap hours in a Double, then use toString() on that Double
hoursminfield = Double.valueOf(hours).toString(); 

// or else a different way to wrap in a double using the Double constructor:
(new Double(hours)).toString(); 

// or else use the very helpful valueOf() method from the class String which will 
// create a string version of any primitive type:
hoursminfield = String.valueOf(hours); 



原始答案(在您的代码中解决了另一个问题):



double hours = Mins / 60; 你正在划分两个 int s。您将获得该部门的 int 值,因此如果
Mins = 43;
double hours = Mins / 60;
// Mins / 60是一个int = 0.将它分配到两个小时使
//小时一个等于零。

ORIGINAL ANSWER (addressed a different problem in your code):

In double hours = Mins / 60; you are dividing two ints. You will get the int value of that division, so if Mins = 43; double hours = Mins / 60; // Mins / 60 is an int = 0. assigning it to double hours makes // hours a double equal to zero.

什么你需要做的是:

double hours = Mins / ((double) 60);

或类似的东西,您需要将您的部门的一部分投入到一个 double 为了强制使用 double s而不是 int s。

or something like that, you need to cast some part of your division to a double in order to force the division to be done with doubles and not ints.

这篇关于双重不能被取消引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆