求教Java高手

相约钓鱼, 探讨钓技, 结交钓友, 分享渔获 。。。

求教Java高手

楼层:#1  帖子肥彁 » 20 2月 2014 20:55

刚学用Java,我有下面的一段程序,如果s的值(value)是空字符串,或者不是数字字符串,我这样查d有没有问题?
string s = ''; --s 是个变量
Double d = Double.valueOf(s);
if(d!=null & d>0.001){...
}

my question is: if s is string.empty or not a number, is d null? or when i do Double.valueOf(s), already get exception? two lazy, i should create a unit test for this.

谢谢。
最后由 肥彁 编辑于 20 2月 2014 22:12,总共编辑了 1 次
肥彁
Lieutenant General (中将)
Lieutenant General (中将)
 
帖子: 2341
注册: 02 10月 2013 16:56
Member Number (论坛会员注册序列号): #81
地址: Carlton & Manhattan, Markham

Re: 求教Java高手

楼层:#2  帖子xiaolu » 20 2月 2014 21:21

肥彁 写道:刚学用Java,我有下面的一段程序,如果s的值(value)是空字符串,或者不是数字字符串,我这样查d有没有问题?
string s = ''; --s 是个变量
Double d = Double.valueOf(s);
if(d!=null & d>0.001){...
}

谢谢。

先声明, 俺不是JAVA高手, 基本从来没写过JAVA的东西, 但是:

为什么使用 0.001? 感觉应当是 if(d!=null & d>0.0) (此测法也有问题, 因为 0.0 也是个合理的 DOUBLE 的数值啊)


或者直接的: find out if everything in s is a digit (0,1,2,3,4,5,6,7,8,9), or + or -, or decimal point (.)? ( + or -1 if any must be first char in s, and at most one decimal but not last char in s). C/C++ 有 function: isdigit(). JAVA 有没类似的?
xiaolu
Site Admin (站长)
 
帖子: 42778
注册: 17 9月 2013 06:22
Member Number (论坛会员注册序列号): #1
地址: Maple, Vaughan

Re: 求教Java高手

楼层:#3  帖子icedeer » 20 2月 2014 21:25

不是JAVA高手,但想帮,只是不太清楚要测什么?

String s;
if(s== null || s.trim().length() ==0 || s.matches("-?\\d+(\\.\\d+)?"){
}
icedeer
Major General (少将)
Major General (少将)
 
帖子: 625
注册: 08 10月 2013 16:47
Member Number (论坛会员注册序列号): #123
地址: Steeles & HWY 404, Markham

Re: 求教Java高手

楼层:#4  帖子xiaolu » 20 2月 2014 21:29

icedeer 写道:不是JAVA高手,但想帮,只是不太清楚要测什么?

String s;
if(s== null || s.trim().length() ==0 || s.matches("-?\\d+(\\.\\d+)?"){
}

估计是说, 想测 string s 是否是个十进制的小数, 比如:

1.23
-0.35
345
.23
+23.65
+.541
-.890
-40
0.0
xiaolu
Site Admin (站长)
 
帖子: 42778
注册: 17 9月 2013 06:22
Member Number (论坛会员注册序列号): #1
地址: Maple, Vaughan

Re: 求教Java高手

楼层:#5  帖子为什么不能字母 » 20 2月 2014 23:51

我不是java高手,但java和c是我唯一会的两门语言

你的d是double型变量,不存在null - 初始值大概是0.000。
你的s是String型变量,你调用的valueOf这个方法是String Class里的,或者是int类里的,刚google了半天,找不到double里有这么个方法。所以。。。这个我不清楚。。。明天可以上班的时候run一下。

如果你这么写是可以的
double d = 0.0001;
String s = '';
s = String.valueOf(d);
System.out.println(s); //你这行输出的应该是0.0001,这是String,不是数值。

=================================================================================================
String s = ''; //String的S大写
double d;//double的d小写
d = (double)(valueOf(s));
if(d!=null && d>0.001){...//逻辑和用双&
}
麻烦您把要求说的详细一点,我现在有点看不懂这题目要干什么。。。
为什么不能字母
General (上将)
General (上将)
 
帖子: 4381
注册: 19 9月 2013 21:37
Member Number (论坛会员注册序列号): #47

Re: 求教Java高手

楼层:#6  帖子为什么不能字母 » 20 2月 2014 23:58

刚查了有这么个好东西叫parseDouble(String s)
这里有两行代码儿,明天还是上班的时候可以验证一下~~~
String str = "123.67";
double d = parseDouble(str);
为什么不能字母
General (上将)
General (上将)
 
帖子: 4381
注册: 19 9月 2013 21:37
Member Number (论坛会员注册序列号): #47

Re: 求教Java高手

楼层:#7  帖子边走边钓 » 20 2月 2014 23:59

路过的电脑盲对楼上的几位的佩服和敬仰如滔滔江水绵绵不绝。 :up :up :up :up :up
边走边钓
Grand General (大将)
Grand General (大将)
 
帖子: 16832
注册: 17 9月 2013 08:15
Member Number (论坛会员注册序列号): #4
地址: Oakville
称呼: 边部长

Re: 求教Java高手

楼层:#8  帖子liugh188 » 21 2月 2014 00:18

景仰啊,万能的鱼坛无所不能啊! :up
liugh188
Senior Captain (大尉)
 
帖子: 56
注册: 26 10月 2013 20:02
Member Number (论坛会员注册序列号): #149

Re: 求教Java高手

楼层:#9  帖子wenxiuxuan » 21 2月 2014 09:51

这样写行不行

d = s!= null ? Double.parseDouble(s) : 0.0;
wenxiuxuan
General (上将)
General (上将)
 
帖子: 10048
注册: 17 9月 2013 08:03
Member Number (论坛会员注册序列号): #3
地址: Richmond Hill

Re: 求教Java高手

楼层:#10  帖子Pungo » 21 2月 2014 10:25

liugh188 写道:景仰啊,万能的鱼坛无所不能啊! :up


如果这个问题没人回答,那才怪了。
俺接触的人,感觉上,挨踢人士占一半以上。
本论坛也是,大多数也是挨踢。 :shake
Pungo
General (上将)
General (上将)
 
帖子: 10132
注册: 17 9月 2013 10:10
Member Number (论坛会员注册序列号): #10
地址: Richmond Hill
称呼: 司令

Re: 求教Java高手

楼层:#11  帖子肥彁 » 21 2月 2014 10:28

再次感谢。我会写些codes在hello world验证下。这些程序会在kettle etl 上用的,还是另外写个小程序测试一下好。
肥彁
Lieutenant General (中将)
Lieutenant General (中将)
 
帖子: 2341
注册: 02 10月 2013 16:56
Member Number (论坛会员注册序列号): #81
地址: Carlton & Manhattan, Markham

Re: 求教Java高手

楼层:#12  帖子xiaolu » 21 2月 2014 10:43

胖Pungo狗 写道:
liugh188 写道:景仰啊,万能的鱼坛无所不能啊! :up


如果这个问题没人回答,那才怪了。
俺接触的人,感觉上,挨踢人士占一半以上。
本论坛也是,大多数也是挨踢。 :shake

嗯, 简称"挨踢民工" :-)
xiaolu
Site Admin (站长)
 
帖子: 42778
注册: 17 9月 2013 06:22
Member Number (论坛会员注册序列号): #1
地址: Maple, Vaughan

Re: 求教Java高手

楼层:#13  帖子jackliu80 » 21 2月 2014 10:47

肥彁 写道:刚学用Java,我有下面的一段程序,如果s的值(value)是空字符串,或者不是数字字符串,我这样查d有没有问题?
string s = ''; --s 是个变量
Double d = Double.valueOf(s);
if(d!=null & d>0.001){...
}

my question is: if s is string.empty or not a number, is d null? or when i do Double.valueOf(s), already get exception? two lazy, i should create a unit test for this.

谢谢。


1. Double.valueOf(s) should throw an exception: NumberFormatException - if the string does not contain a parsable number.
2. To check if it's a number or digit, use apache-commons: NumberUtils.isNumber(s) or NumberUtils.isDigits()
3. To convert a string to double/float/int, use the same library: NumberUtils.toDouble(s) will return 0d if s is not a number string, or NumberUtils.toDouble(s, defaultDoubleValue) will return defaultDoubleValue if s is not a number string
jackliu80
Lieutenant General (中将)
Lieutenant General (中将)
 
帖子: 1523
注册: 18 9月 2013 10:12
Member Number (论坛会员注册序列号): #35
地址: Major Mac & Woodbine, Markham

Re: 求教Java高手

楼层:#14  帖子xiaolu » 21 2月 2014 10:51

jackliu80 写道:
肥彁 写道:刚学用Java,我有下面的一段程序,如果s的值(value)是空字符串,或者不是数字字符串,我这样查d有没有问题?
string s = ''; --s 是个变量
Double d = Double.valueOf(s);
if(d!=null & d>0.001){...
}

my question is: if s is string.empty or not a number, is d null? or when i do Double.valueOf(s), already get exception? two lazy, i should create a unit test for this.

谢谢。


1. Double.valueOf(s) should throw an exception: NumberFormatException - if the string does not contain a parsable number.
2. To check if it's a number or digit, use apache-commons: NumberUtils.isNumber(s) or NumberUtils.isDigits()
3. To convert a string to double/float/int, use the same library: NumberUtils.toDouble(s) will return 0d if s is not a number string, or NumberUtils.toDouble(s, defaultDoubleValue) will return defaultDoubleValue if s is not a number string


嗯, 介个真正"佳娃"高手来也... :shake :up :laugh3
xiaolu
Site Admin (站长)
 
帖子: 42778
注册: 17 9月 2013 06:22
Member Number (论坛会员注册序列号): #1
地址: Maple, Vaughan

Re: 求教Java高手

楼层:#15  帖子wenxiuxuan » 21 2月 2014 11:51

parseDouble returns a primitive double containing the value of the string:
Returns a new double initialized to the value represented by the specified String, as performed by the valueOf method of class Double.

valueOf returns a Double instance, if already cached, you'll get the same cached instance.
Returns a Double instance representing the specified double value. If a new Double instance is not required, this method should generally be used in preference to the constructor Double(double), as this method is likely to yield significantly better space and time performance by caching frequently requested values.

To avoid the overhead of creating a new Double object instance, you should normally use valueOf.

So , for eg.

double d = Double.parseDouble("1");

Double d = Double.valueOf("1");
wenxiuxuan
General (上将)
General (上将)
 
帖子: 10048
注册: 17 9月 2013 08:03
Member Number (论坛会员注册序列号): #3
地址: Richmond Hill

Re: 求教Java高手

楼层:#16  帖子wenxiuxuan » 21 2月 2014 11:54

Double.parseDouble(null); // throws java.lang.NullPointerException

Integer.parseInt(null); // throws java.lang.NumberFormatException: null
wenxiuxuan
General (上将)
General (上将)
 
帖子: 10048
注册: 17 9月 2013 08:03
Member Number (论坛会员注册序列号): #3
地址: Richmond Hill

Re: 求教Java高手

楼层:#17  帖子墨垨塵龜❀阳光 » 21 2月 2014 11:57

1=1, 2=2 1+1=4, 1+1=3
求证1有多2
墨垨塵龜❀阳光
Brigadier General (大校/准将)
Brigadier General (大校/准将)
 
帖子: 368
注册: 17 9月 2013 14:19
Member Number (论坛会员注册序列号): #15
地址: Yonge&Finch,North York

Re: 求教Java高手

楼层:#18  帖子肥彁 » 21 2月 2014 12:04

ok啦,还是不能偷懒啊。

正如上面高手所说的,字符串不是数字时,是会有exception的。但我不会调用IsNumber的东东,所以变成下面的应对方法。 :laugh3

public class Test1 {
public static void main(String args[]){
String s = "error";
double d = 0.1;
try
{
d = Double.parseDouble(s);
}
catch(Exception e)
{
}

System.out.println(d);

}
}
肥彁
Lieutenant General (中将)
Lieutenant General (中将)
 
帖子: 2341
注册: 02 10月 2013 16:56
Member Number (论坛会员注册序列号): #81
地址: Carlton & Manhattan, Markham


回到 It's All About Fishin' 钓鱼!

在线用户