Domanda

I am new to Java and am still getting used to the minor difference so please excuse any mistakes you may find ridiculous.

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. It then parses the string and truncates the C or F and stores the values contained in the strings as floats. I am using Eclipse and the program is object oriented, this is my code:

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

}
È stato utile?

Soluzione

the exception you are getting is beacuse you passed '0.0C' to the float parser at:

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

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
  • after that the Temperature class's constructor should be responsible for parsing '0.0C' and '42.0F'

Altri suggerimenti

It is better you take inputs as <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. Even better, just take <temp1> <temp2> as you command line arguments and decide that <temp1> is in degC and <temp2> is in F.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top