2013-07-13 118 views
0

我是Java新手,仍在习惯于细微差别,所以请原谅您可能会觉得荒谬的任何错误。尝试在Java中使用命令行参数时出错

我想写一个程序,存储温度,可用于调用摄氏温度或华氏温度。我唯一的问题自带的命令行参数,在成功编译我的节目,我输入以下内容:

java Driver 0.0C 32.0F 

然后我得到这个:

Exception in thread "main" java.lang.NumberFormatException: For input string: 
"0.0C" 
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1241) 
    at java.lang.Float.parseFloat(Float.java:452) 
    at Driver.main(Driver.java:47) 

我的程序还没有完全磨合了,所以我知道吸气剂可以写得非常有效,驱动程序甚至不会调用温度等级,但目前这不是我关心的问题。我的驱动程序应该接受输入并根据“C”或“F”字符确定值是以摄氏度还是华氏度表示。然后解析字符串并截断C或F,并将包含在字符串中的值存储为浮点数。我使用Eclipse和程序是面向对象的,这是我的代码:

public class Temperature { 

    private float temperature; 
    private char scale; 

    // default constructor 
    Temperature() { 
     this.temperature = 0; 
     this.scale = 'C'; 
    } 

    Temperature(float temperatureIn) { 
     this.temperature = temperatureIn; 
     this.scale = 'C'; 
    } 

    Temperature(char scaleIn) { 
     this.temperature = 0; 
     this.scale = scaleIn; 
    } 

    Temperature(float temperatureIn, char scaleIn) { 
     this.temperature = temperatureIn; 
     this.scale = scaleIn; 
    } 

    float degreesC(float degreesF) { 
     float degreesC = (5 * (degreesF - 32))/9; 
     return degreesC; 
    } 

    float degreesF(float degreesC) { 
     float degreesF = (9*(degreesC/5)) + 32; 
     return degreesF; 
    } 

    void setTemperature(float temperatureIn) { 
     temperature = temperatureIn; 
    } 

    void setScale(char scaleIn) { 
     scale = scaleIn; 
    } 

    void setBothValues(float temperatureIn, char scaleIn) { 
     temperature = temperatureIn; 
     scale = scaleIn; 
    } 

    int compareTemps(Temperature temp1, Temperature temp2) { 

     // both values will be compared in Farenheit 
     Temperature temp1temp = temp1; 
     if (temp1temp.scale == 'C') { 
      temp1temp.temperature = degreesF(temp1temp.temperature); 
      temp1temp.scale = 'F'; 
     } 

     Temperature temp2temp = temp2; 
     if (temp2temp.scale == 'C') { 
      temp2temp.temperature = degreesF(temp2temp.temperature); 
      temp2temp.scale = 'F'; 
     } 

     if (temp1temp.temperature == temp2temp.temperature) { 
      return 0; 
     } 

     if (temp1temp.temperature > temp2temp.temperature) 
      return 1; 

     if (temp1temp.temperature < temp2temp.temperature) 
      return -1; 

     return 0; 
    } 
} 

和主驱动程序:

public class Driver { 

public static void main(String[] args) { 

    // ints to hold the temperature values 
    float temp1Value = 0; 
    float temp2Value = 0; 

    // strings to hold the scale types 
    char temp1Scale = 'C'; 
    char temp2Scale = 'C'; 

    // declare objects of type temperature 
    Temperature firstTemp = null; 
    Temperature secondTemp = null; 


    // copy scale values of temperatures 
    int scaleIndex = 0; 
    int scaleIndex2 = 0; 
    if (args.length > 0) { 
     if (args[0].indexOf('C') != -1) 
     { 
      scaleIndex = args[0].indexOf('C'); 
      temp1Scale = args[0].charAt(scaleIndex); 
     } 
     else if (args[0].indexOf('F') != -1) 
     { 
      scaleIndex = args[0].indexOf('F'); 
      temp1Scale = args[0].charAt(scaleIndex); 
     } 

     if (args[1].indexOf('C') != -1) 
     { 
      scaleIndex = args[1].indexOf('C'); 
      temp2Scale = args[1].charAt(scaleIndex2); 
     } 
     else if (args[1].indexOf('F') != -1) 
     { 
      scaleIndex = args[1].indexOf('F'); 
      temp2Scale = args[1].charAt(scaleIndex2); 
     } 
    } 

    // parse the values to exclude scales and copy to strings holding temperature values 
    if (args.length > 0) { 
     temp1Value = Float.parseFloat(args[0].substring(0, scaleIndex)); 
     temp2Value = Float.parseFloat(args[1].substring(0, scaleIndex2)); 
    } 
} 

} 
+0

错误告诉你到底发生了什么错误,NumberFormatException:对于输入字符串: “0.0C”'。您试图将“0.0C”解析为数字,除非数字是十六进制数字(不是),否则数字中没有像“C”这样的字母。解决方案:不要尝试解析字母。在解析之前摆脱这些字母。 –

+0

'if(args [0] .indexOf('C')!= -1){scaleIndex = args [0] .indexOf('C'); temp1Scale = args [0] .charAt(scaleIndex); }' - 你已经在括号内的点处建立了比例尺'C'。您不需要再从输入中重新获取它。 'args [0] .charAt(scaleIndex)'不能只是''C'' –

回答

0

你所得到的例外是怎么一回事,因为你在通过“0.0C”的花车解析器:

tempValue = Float.parseFloat(args[1].substring(0, scaleIndex)); 

这是怎么一回事,因为你做

scaleIndex = args[1].indexOf('F'); 

有效覆盖scaleIndex,而不是设置scaleIndex2

请开放的态度与我的建议如下:

  • 面向对象意味着你创造这将占用责任
  • 你的体温类存储温度在摄氏和华氏too..which可能更容易,但存储类只有例如Kelvins意味着你有一个强烈的内部概念
  • 当有人要求C或F它计算从K
  • 之后,温度类的构造函数应该负责解析'0.0C'和' 42.0F'
+0

谢谢你刚才解决了我的问题,我绝对会考虑你的建议。 – mudejar

0

最好是你把投入作为<temp1> <unit1> <temp2> <unit2>。通过这种方式,您将以所需的格式获得所需的所有参数。现在,您可以分析args[0]args[2]以获取tempValues和其他两个参数。更妙的是,只是把<temp1> <temp2>当你命令行参数,并决定<temp1>是℃且<temp2>是F.