简体   繁体   English

尝试在Java中使用命令行参数时出错

[英]Error trying to use command line arguments in Java

I am new to Java and am still getting used to the minor difference so please excuse any mistakes you may find ridiculous. 我是Java的新手,并且仍会适应微小的差异,因此,请原谅任何可能会荒谬的错误。

I am trying to write a program that stores temperature and can be used to call that temperature in Celsius or in Fahrenheit. 我正在尝试编写一个存储温度的程序,可用于在摄氏或华氏温度下调用该温度。 My only issue comes with the command line arguments, after successfully compiling my program I enter the following: 我唯一的问题是命令行参数,在成功编译程序后,我输入以下内容:

java Driver 0.0C 32.0F

And then I get this: 然后我得到这个:

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)

My program is still not completely polished up so I know that the getters can be written to be much for efficient and that the driver program doesn't even call the temperature class, but this is not my concern at the moment. 我的程序仍未完全完善,因此我知道可以将吸气剂编写得非常有效率,并且驱动程序甚至都没有调用温度等级,但这不是我目前关心的问题。 My Driver is supposed to take in the input and determine from the 'C' or 'F' character whether the value is in Celsius or Fahrenheit. 我的驱动程序应该接受输入,并根据“ C”或“ F”字符确定该值是摄氏度还是华氏度。 It then parses the string and truncates the C or F and stores the values contained in the strings as floats. 然后,它解析字符串并截断C或F,并将字符串中包含的值存储为浮点数。 I am using Eclipse and the program is object oriented, this is my code: 我正在使用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;
    }
} 

And the main driver program: 和主驱动程序:

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));
    }
}

}

It is better you take inputs as <temp1> <unit1> <temp2> <unit2> . 最好将输入作为<temp1> <unit1> <temp2> <unit2> This way you'll get all the parameter you need in the desired format. 这样,您将以所需的格式获得所需的所有参数。 You can now parse args[0] and args[2] for tempValues and the other two parameter for the units. 现在,您可以解析args[0]args[2]的tempValues以及其他两个参数的单位。 Even better, just take <temp1> <temp2> as you command line arguments and decide that <temp1> is in degC and <temp2> is in F. 更好的是,在命令行参数中使用<temp1> <temp2> ,并确定<temp1>在degC中,而<temp2>在F中。

the exception you are getting is beacuse you passed '0.0C' to the float parser at: 您收到的异常是因为您在以下位置将'0.0C'传递给了浮动分析器:

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

that is beacuse you do 那是因为你这样做

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

effectively overwriting the scaleIndex instead of setting scaleIndex2 有效覆盖scaleIndex而不是设置scaleIndex2

please be open minded with my following recommendations: 请对以下建议持开放态度:

  • object oriented means you create classes which will take up responsibility 面向对象意味着您将创建负责的类
  • your Temperature class stores temp in celsius and in fahrenheit too..which might be easier, but storing only for example Kelvins would mean you have a strong inner concept inside the class 您的温度班级也将温度存储在摄​​氏温度和华氏温度下。这可能更容易,但是仅存储例如开尔文斯就意味着您在班级内部有很强的内在观念
  • when someone asks for C or F it calculates from the K 当有人要求C或F时,它将根据K进行计算
  • after that the Temperature class's constructor should be responsible for parsing '0.0C' and '42.0F' 之后,温度类的构造函数应负责解析“ 0.0C”和“ 42.0F”

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

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