分页: 1 / 1

求教Java高手

帖子发表于 : 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.

谢谢。

Re: 求教Java高手

帖子发表于 : 20 2月 2014 21:21
xiaolu
肥彁 写道:刚学用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 有没类似的?

Re: 求教Java高手

帖子发表于 : 20 2月 2014 21:25
icedeer
不是JAVA高手,但想帮,只是不太清楚要测什么?

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

Re: 求教Java高手

帖子发表于 : 20 2月 2014 21:29
xiaolu
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

Re: 求教Java高手

帖子发表于 : 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){...//逻辑和用双&
}
麻烦您把要求说的详细一点,我现在有点看不懂这题目要干什么。。。

Re: 求教Java高手

帖子发表于 : 20 2月 2014 23:58
为什么不能字母
刚查了有这么个好东西叫parseDouble(String s)
这里有两行代码儿,明天还是上班的时候可以验证一下~~~
String str = "123.67";
double d = parseDouble(str);

Re: 求教Java高手

帖子发表于 : 20 2月 2014 23:59
边走边钓
路过的电脑盲对楼上的几位的佩服和敬仰如滔滔江水绵绵不绝。 :up :up :up :up :up

Re: 求教Java高手

帖子发表于 : 21 2月 2014 00:18
liugh188
景仰啊,万能的鱼坛无所不能啊! :up

Re: 求教Java高手

帖子发表于 : 21 2月 2014 09:51
wenxiuxuan
这样写行不行

d = s!= null ? Double.parseDouble(s) : 0.0;

Re: 求教Java高手

帖子发表于 : 21 2月 2014 10:25
Pungo
liugh188 写道:景仰啊,万能的鱼坛无所不能啊! :up


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

Re: 求教Java高手

帖子发表于 : 21 2月 2014 10:28
肥彁
再次感谢。我会写些codes在hello world验证下。这些程序会在kettle etl 上用的,还是另外写个小程序测试一下好。

Re: 求教Java高手

帖子发表于 : 21 2月 2014 10:43
xiaolu
胖Pungo狗 写道:
liugh188 写道:景仰啊,万能的鱼坛无所不能啊! :up


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

嗯, 简称"挨踢民工" :-)

Re: 求教Java高手

帖子发表于 : 21 2月 2014 10:47
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

Re: 求教Java高手

帖子发表于 : 21 2月 2014 10:51
xiaolu
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

Re: 求教Java高手

帖子发表于 : 21 2月 2014 11:51
wenxiuxuan
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");

Re: 求教Java高手

帖子发表于 : 21 2月 2014 11:54
wenxiuxuan
Double.parseDouble(null); // throws java.lang.NullPointerException

Integer.parseInt(null); // throws java.lang.NumberFormatException: null

Re: 求教Java高手

帖子发表于 : 21 2月 2014 11:57
墨垨塵龜❀阳光
1=1, 2=2 1+1=4, 1+1=3
求证1有多2

Re: 求教Java高手

帖子发表于 : 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);

}
}