Java字符串转换问题!急!!!!

来源:百度知道 编辑:UC知道 时间:2024/05/24 16:47:06
接收JSP页面传过来的字符串类型怎么转换成Long型(注意不是long型),如:
test.setTestedition(new Long(Long.parseLong(request.getAttribute("testedtion"))));

test.setTestedition(new Long(Long.parseLong(request.getParameter("testedtion"))));
这样都不对的。请高手指教,谢谢啦!!
test是我写的类的对象,这个甭用管,我们现在只管怎么接受传过来的值就行了。(报编译错误)。
其中"testedtion"是传过来的字符类型的变量,需要把它转换成Long型给setTestedition接收就可以了,其他的不需要讨论了,谢谢了。

最好写个实例出来。

声明一下,testedtion传过去的肯定是纯数字的了,但还是提示“文件编译错误!”,如果采用:
test.setTestedition(new Long(28L)); 是没有问题的,但是目前不是设一个死的值而是接收传值,各位有什么好的办法?!

在java代码中只能用
request.getParameter;
还有 你的异常是:文件编译错误,可能是代码有错误,也可能是jdk版本的问题,(你的项目jdk和workspace设定的编译版本不一致),
另外
test.setTestedition(new Long(Long.parseLong(request.getParameter("testedtion")))); 这个要是有异常 是NumberFormatException,也就是字符的异常,不会是编译的错啊..

就用包装类的paseXxx方法可以搞定吧

Long long=Long.parseLong(request.getParameter("testedtion"))));
试试~

public Long(String s) throws NumberFormatException
有这个构造方法的
test.setTestedition(new Long(request.getAttribute("testedtion")));

看 JDK API 怎么说:
parseLong
public static long parseLong(String s)
throws NumberFormatExceptionParses the string argument as a signed decimal long. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' (\u002D') to indicate a negative value. The resulting long value is returned, exactly as if the argument and the radix 10 were give