简体   繁体   English

java字符串值应该在double.valueof(..)中使用

[英]java string value should use in double.valueof(..)

I am creating .jsp page and reiceve error about it ; 我正在创建.jsp页面并对其进行reiceve错误;

        String energyv="";

        int number= Integer.parseInt(request.getParameter("number"));
        energyv="_"+number;
    *
    *
    *
        if (tokenNumber == 2 && found && value1.equals("")) {
        value1= nextToken;
        if (value1.equals(""))
        found = false;

        }

        if (tokenNumber == 3 && found && value2.equals("")) {
        value2= nextToken;

        }

*
*
*
        Double activeTotalLong = Double.valueOf( "value" + energyv );

*
*
*

When try to do this I recieve tomcat error; 当我尝试这样做时,我收到了tomcat错误;

02 Aug 2012 11:17:30,097 ERROR http-8080-4 org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/PlantVisorPRO].[jsp] - Servlet.service() for servlet jsp threw exception java.lang.NumberFormatException: For input string: "value2" at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1224) at java.lang.Double.valueOf(Double.java:475) 2012年8月2日11:17:30,097错误http-8080-4 org.apache.catalina.core.ContainerBase。[Catalina]。[localhost]。[/ PlantVisorPRO]。[jsp] - Servlet.service()for servlet jsp thrrew exception java.lang.NumberFormatException:对于输入字符串:“value2”at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1224)at java.lang.Double.valueOf(Double.java:475)

Java can create my value ( like a value2 or value1), but how can i fix string problem ? Java可以创建我的值(如value2或value1),但我如何修复字符串问题?

Double activeTotalLong = Double.valueOf( "value" + energyv );

This method takes valid numbers represented in a string. 此方法采用字符串表示的有效数字。

"value2" isn't a valid number format. “value2”不是有效的数字格式。

Example usages of Double.valueOf(): Double.valueOf()的示例用法:

Double d;
d = Double.valueOf("23.72");
d = Double.valueOf("-1.247002");
d = Double.valueOf("1e9"); // e  =    *10^()

So, what you are trying to do is, get the value of value2 or value1 . 所以,你要做的是获取value2value1 And what you actually wrote is 而你实际写的是

   "value" + 2
=  "value2"    // This is a string! not a variable!

So, to solve this, there are two options: 因此,要解决这个问题,有两种选择:

  • use arrays 使用数组
  • use if-else 使用if-else

Using arrays: 使用数组:

String values[] = new String[2];
values[0] = value1;
values[1] = value2;
Double.valueOf(values[energyv - 1]);

Using if-else: 使用if-else:

Double activeTotalLong;
if (energyv == 1)
{
    activeTotalLong = Double.valueOf(value1);
} else if (energyv == 2)
{
    activeTotalLong = Double.valueOf(value2);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM