0

I'm doing a project with command line and array. A program that converts F -> C and C -> F. Here what I got so far:

public class Implementation 
{
    public static void main(String[] args)
    {        
        double degree = 0;
        String celsius = null;
        String fahrenheit; 
        int n = 0;
        String[] days = {"Very Cold", "Cold", "Mild", "Very Mild", "Warm", "Very Warm", "Hot"};       
            if (degree < 0)
            {    
            n = 0;
            }
            if (degree >= 0 && degree < 32)
            {    
            n = 1;
            }
            if (degree >= 32 && degree < 50)
            {    
            n = 2;
            }
            if (degree >= 50 && degree < 60)
            {    
            n = 3;
            }
            if (degree >= 60 && degree < 70)
            {    
            n = 4;
            }
            if (degree >= 70 && degree < 90)
            {    
            n = 5;
            }
            if (degree >= 90)
            {    
            n = 6;
            }   
        if (args.length != 3)
        {
            System.out.println("Error! Please try again.");
            System.exit(0);
        }
        else
        {

            degree = Double.parseDouble(args[0]);
            celsius = args[1]; 
            fahrenheit = args[2];        
        }

        switch (celsius)    
        {
            case "c":
            System.out.printf("%n%s Celsius is %.5s Fahrenheit %s\n", args[0], fahrenheit(degree), days[n]);
            break;
            case "f":
            System.out.printf("%n%s Fahrenheit is %.5s Celsius %s\n", args[0], celsius(degree), days[n]);
            break;
        }
    }
    public static double celsius(double fahrenheitTemperature)
    {
        return ( 5.0 / 9.0 * ((double) fahrenheitTemperature - 32));
    }
    public static double fahrenheit(double celsiusTemperature)
    {
    return  ( 9.0 / 5.0 * (double) celsiusTemperature + 32);
    }
}

I have problem in Array part. I couldn't get the right array days[] with the specific degree. For example, If I do command line: 100 c f, it supposes to show up 100 Celsius is 212.0 Fahrenheit Cold "Hot". But my program only shows up "Cold", no matter what degree I input.

1
  • it looks like you set n before you get the args, so n defaults to 0. Actually n will be 1, because degree = 0 and 0 degrees makes n = 1. Commented Mar 17, 2015 at 22:12

2 Answers 2

1

you set

double degree = 0;

then

String[] days = {"Very Cold", "Cold", "Mild", "Very Mild", "Warm", "Very Warm", "Hot"};    

so of course it will result in "Cold" all the time...

Here is a fully working class:

As you can see in my solution the whole point is to create a function which returns the proper integer, depending on the degree you provide as input.. this function is called public static int checkDegree(double degree) { . This way you put the functionality in a function and remove it from the main class (which is very problematic the way you had it). this way you call the function as an argument directly, and the function gives you proper days you need for the logic you request, instead of calling the days[n] directly, which was causing the troubles you were faced with in the first place...

public class Implementation 
{
    public static void main(String[] args)
    {        
        double degree=0;
        String celsius=null;
        String fahrenheit;
        int n;
        String[] days = {"Very Cold", "Cold", "Mild", "Very Mild", "Warm", "Very Warm", "Hot"}; 
        if (args.length != 3)
        {
            System.out.println("Error! Please try again.");
            System.exit(0);
        }
        else
        {
            degree = Double.parseDouble(args[0]);
            celsius = args[1]; 
            fahrenheit = args[2];        
        }

        switch (celsius)    
        {
            case "c":    
            System.out.printf("%n%s Celsius is %.5s Fahrenheit %s\n", args[0], fahrenheit(degree), days[checkDegree(degree)]);
            break;
            case "f":
            System.out.printf("%n%s Fahrenheit is %.5s Celsius %s\n", args[0], celsius(degree), days[checkDegree(degree)]);
            break;
        }
    }
    public static double celsius(double fahrenheitTemperature)
    {
        return ( 5.0 / 9.0 * ((double) fahrenheitTemperature - 32));
    }
    public static double fahrenheit(double celsiusTemperature)
    {
    return  ( 9.0 / 5.0 * (double) celsiusTemperature + 32);
    }
    public static int checkDegree(double degree) {
        int myReturn = 0;
        if (degree < 0)
            {    
            myReturn = 0;
            }
            if (degree >= 0 && degree < 32)
            {    
            myReturn = 1;
            }
            if (degree >= 32 && degree < 50)
            {    
            myReturn = 2;
            }
            if (degree >= 50 && degree < 60)
            {    
            myReturn = 3;
            }
            if (degree >= 60 && degree < 70)
            {    
            myReturn = 4;
            }
            if (degree >= 70 && degree < 90)
            {    
            myReturn = 5;
            }
            if (degree >= 90)
            {    
            myReturn = 6;
            }   
            return myReturn;
    }
}

INPUT : java Implementation 100 c f

OUTPUT : 100 Celsius is 212.0 Fahrenheit Hot

Sign up to request clarification or add additional context in comments.

6 Comments

If I don't initialize degree = 0, it won't work. The program makes me initialize degree. What should I do?
See my edit. I posted you a fully working code which produces the result you want
This actually worked. Thank you very much. I just realize that checkDegree need to be done for Fahrenheit degree (when convert from C to F). For example, when I do command line like 10 c f, it supposes to be Mild (not Cold). How could we fix this?
Never mind, I found it. Thank you very much. You are very nice. This made my day :D
Glad it worked. If you want to change what appears for given degrees, just change the if statements and the text you select to appear for every given degree...
|
1

Your

double degree = 0;

remains always 0. You change it after your if statements so it is always 0.

You can change this:

switch (celsius)    
        {
            case "c":
            System.out.printf("%n%s Celsius is %.5s Fahrenheit %s\n", args[0], fahrenheit(degree), days[n]);
            break;
            case "f":
            System.out.printf("%n%s Fahrenheit is %.5s Celsius %s\n", args[0], celsius(degree), days[n]);
            break;
        }

to this:

switch (celsius)    
        {
            case "c":
            System.out.printf("%n%s Celsius is %.5s Fahrenheit ", args[0], fahrenheit(degree));
            break;
            case "f":
            System.out.printf("%n%s Fahrenheit is %.5s Celsius ", args[0], celsius(degree));
            break;
        }

and move it just right above your degrees declaration. Move this part too:

if (args.length != 3)
        {
            System.out.println("Error! Please try again.");
            System.exit(0);
        }
        else
        {

            degree = Double.parseDouble(args[0]);
            celsius = args[1]; 
            fahrenheit = args[2];        
        }

Then at the end of your main() add this:

System.out.printf("%s\n", days[n]);

That's the quickest fix I can imagine. Of course there are about a million more elegant ways to do what you want but it goes out of this answer's scope. I strongly recommend you to watch some programming tutorials or even better read a book.

1 Comment

What can I do to fix it? Becausse If I do not set double degree = 0, the program won't work. Sorry I'm new to coding

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.