Registered User
Registered User

Reputation: 474

Returning a variable from an if-else statement to the main program

I am writing a program which gives the cmyk output when you put in rgb values.

I did Python before Java, so I'm currently a java noob.
Apparently variables within if-else statments can't be passed onto the main program. In blueJ, it says "cannot find variable c"

public class color_conversion
{
    public static void main(String [] args)
    {
        int r = Integer.parseInt(args[0]);
        int g = Integer.parseInt(args[1]);
        int b = Integer.parseInt(args[2]);
        if ((r == 0 && g == 0)&&(g == 0 && b == 0))
        {
            double c = 0;
            double m = 0;
            double y = 0;
            double k = 0;
        }
        else         
        {
            int w = (int)(Math.max(r/255,(Math.max(g/255,b/255))));
            double c = (double)((w-(r/255))/w);
            double m = (double)((w-(g/255))/w);
            double y = (double)((w-(b/255))/w);
            double k = (double)(1-w);
        }
        System.out.println("cyan    =" + c);
        System.out.println("magenta =" + m);
        System.out.println("yellow  =" + y);
        System.out.println("black   =" + k);
    }
}

What am I doing wrong?

Upvotes: 0

Views: 78

Answers (6)

Pankaj Singhal
Pankaj Singhal

Reputation: 16053

Try declaring the variables outside like this:

public class color_conversion
{
public static void main(String [] args)
{


    double c = 0;
    double m = 0;
    double y = 0;
    double k = 0;
    int r = Integer.parseInt(args[0]);
    int g = Integer.parseInt(args[1]);
    int b = Integer.parseInt(args[2]);
    if (r != 0 || g != 0 || b != 0)        
    {
        int w = (int)(Math.max(r/255,(Math.max(g/255,b/255))));
        c = (double)((w-(r/255))/w);
        m = (double)((w-(g/255))/w);
        y = (double)((w-(b/255))/w);
        k = (double)(1-w);
    }
    System.out.println("cyan    =" + c);
    System.out.println("magenta =" + m);
    System.out.println("yellow  =" + y);
    System.out.println("black   =" + k);
}
}

Upvotes: 1

ashiquzzaman33
ashiquzzaman33

Reputation: 5741

Another problem:

You take integer division like 'r/255', which most of time gives you '0'. To solve this you should cast one of them to 'double', likes

(double)r/255

Or

r/255.0

Or

r/(double)255

or cast both of them.

Upvotes: 1

Someone
Someone

Reputation: 61

In Java all variables and objects have a scope that mean that a variable/object only lives in this scope. In your example, the c variable only exists inside the if statement. When the flow exists from the if statement, the c variable is recollected by GC.

So, just move the c, m, k, y variables at top of main function.

Upvotes: 1

SatyaTNV
SatyaTNV

Reputation: 4135

Declare them out side of if-else block. Other wise compiler will treat them as local variables.

        double c;
        double m;
        double y;
        double k;

 if ((r == 0 && g == 0)&&(g == 0 && b == 0))
    {
        c = 0;
        m = 0;
        y = 0;
        k = 0;
    }
    else         
    {
        int w = (int)(Math.max(r/255,(Math.max(g/255,b/255))));
        c = (double)((w-(r/255))/w);
        m = (double)((w-(g/255))/w);
        y = (double)((w-(b/255))/w);
        k = (double)(1-w);
    }

Upvotes: 1

Sva.Mu
Sva.Mu

Reputation: 1131

You define all the variables c, m, y and k twice - once in if, once in else block, making them local for those blocks only.

You need to define these variables on the same level as r, g and b and just assign values in if or else block.

So it would look like this:

public static void main(String [] args)
{
    int r = Integer.parseInt(args[0]);
    int g = Integer.parseInt(args[1]);
    int b = Integer.parseInt(args[2]);

    double c;
    double m;
    double y;
    double k;

    if ((r == 0 && g == 0)&&(g == 0 && b == 0))
    {
        c = 0;
        m = 0;
        y = 0;
        k = 0;
    }
    else         
    {
        int w = (int)(Math.max(r/255,(Math.max(g/255,b/255))));
        c = (double)((w-(r/255))/w);
        m = (double)((w-(g/255))/w);
        y = (double)((w-(b/255))/w);
        k = (double)(1-w);
    }
    System.out.println("cyan    =" + c);
    System.out.println("magenta =" + m);
    System.out.println("yellow  =" + y);
    System.out.println("black   =" + k);
}

Upvotes: 2

Łukasz
Łukasz

Reputation: 8673

declare your variables before if, as you did with r, g and b. So it would be

double c;
double m;
...

they will be local for whole main method. Now use them inside if without the type (if you put type you would declare new variable local for this if with the same name.) like this

c = 0;

At the moment you made two local sets of c, m, y and k (each local to it's own block)

Upvotes: 1

Related Questions