[英]how to null a variable of type float
My code is working if my float variable is set to 0, but I want my code to work if my variable is null. 如果我的float变量设置为0,我的代码工作,但如果我的变量为null,我希望我的代码工作。 How could I do that? 我怎么能这样做? Here is a code snippet I wrote: 这是我写的代码片段:
float oikonomia= (float)((float)new Float(vprosvasis7.getText().toString()));
if(oikonomia==0){
I have tied if(oikonomia==null)
or if(oikonomia=null)
but it is not working. 我绑了if(oikonomia==null)
或if(oikonomia=null)
但它不起作用。
PS: Another way would be to initially set oikonomia=0;
PS:另一种方式是初始设置oikonomia=0;
and, if the users change it, go to my if session. 并且,如果用户更改它,请转到我的if会话。 This is not working either. 这也不起作用。
Float oikonomia = Float.valueOf(vprosvasis7.getText().toString()); Float oikonomia = Float.valueOf(vprosvasis7.getText()。toString());
if(oikonomia.toString() == " "){
float oikonomia= (float)((float)new Float(vprosvasis7.getText().toString()));
oikonomia=0;
if(oikonomia==0){
that way is not working too: 这种方式也不起作用:
Float oikonomia = Float.valueOf(vprosvasis7.getText().toString());
if(oikonomia.toString() == " "){
If you want a nullable float, then try Float
instead of float
如果你想要一个可以为空的浮点数,那么试试Float
而不是float
Float oikonomia= new Float(vprosvasis7.getText().toString());
But it will never be null
that way as in your example... 但它永远不会像你的例子那样是null
......
EDIT : Aaaah, I'm starting to understand what your problem is. 编辑 :Aaaah,我开始明白你的问题了。 You really don't know what vprosvasis7
contains, and whether it is correctly initialised as a number. 你真的不知道vprosvasis7
包含什么,以及它是否正确初始化为数字。 So try this, then: 所以试试这个,然后:
Float oikonomia = null;
try {
oikonomia = new Float(vprosvasis7.getText().toString());
}
catch (Exception ignore) {
// We're ignoring potential exceptions for the example.
// Cleaner solutions would treat NumberFormatException
// and NullPointerExceptions here
}
// This happens when we had an exception before
if (oikonomia == null) {
// [...]
}
// This happens when the above float was correctly parsed
else {
// [...]
}
Of course there are lots of ways to simplify the above and write cleaner code. 当然,有很多方法可以简化上述内容并编写更清晰的代码。 But I think this should about explain how you should correctly and safely parse a String
into a float
without running into any Exceptions... 但我认为这应该解释如何正确安全地将String
解析为float
而不会遇到任何异常......
Floats and floats are not the same thing. 浮子和浮子不是一回事。 The "float" type in Java is a primitive type that cannot be null. Java中的“float”类型是一种不能为null的基本类型。
You should probably be doing something like this 你可能应该做这样的事情
Float oikonomia = Float.valueOf(vprosvasis7.getText().toString()); Float oikonomia = Float.valueOf(vprosvasis7.getText()。toString());
You're going to get a NumberFormatException if you try to parse a string that doesn't translate into a valid number. 如果您尝试解析未转换为有效数字的字符串,您将获得NumberFormatException。 So you could simply wrap this statement in a try catch block and handle the error properly. 所以你可以简单地将这个语句包装在try catch块中并正确处理错误。
Hope that helps. 希望有所帮助。
try this: 试试这个:
Float oikonomia = null;
try{
oikonomia = Float.valueOf(vprosvasis7.getText().toString());
}catch(numberFormatException e){
// logging goes here
}
Now the value is NULL in case of an illegal (non float) string. 现在,在非法(非浮动)字符串的情况下,该值为NULL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.