0

I have almost completed this code, but it has told me that There is an illegal modifier in the public static int power; line. Can you guys please help?

public static int binToDec(int i)
{
    int[] numbers;//initialize variable
    int f = 4;
    String iString = "" + i;
    int result = 0;
    int length = iString.length();
    public static int power;
    for(power = iString.length(); power>=0;power--)
    {
    while(f == length && f >= 0)
    {

        numbers[power] = iString.charAt(power)^power;
    }

    length--;
    f--;
    }
    for(int g = 0; g <= numbers.length; g++)//double check constraints
    {
        result = numbers[g] = numbers[power];
    }

        return result;
}
2
  • What do you think those modifiers mean? Commented Oct 20, 2014 at 20:58
  • You are aware that Integer.valueOf("1001100101", 2) will do this for you? Commented Oct 20, 2014 at 21:00

5 Answers 5

1

The modifiers public and static have no meaning for a local variable inside a method and are not valid there. The compiler error is suggesting that you remove them.

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

Comments

0

Remove the "public static" in:

public static int power;

Comments

0

In Java, static means that it's a variable or method within a class. In other words if you use static it has to belong to the scope of the whole class. If you move that line of code outside of your method it should fix your problem.

Comments

0

You can do this.

int power = Istring.length(); And remove the initialization from for loop.

Comments

0

Declaring and defining a variable inside a method will limit its scope to the method only. public is a keyword that is used to make it visible to every class out there and static makes the variable shared for whole class.

Therefore, public and static are not allowed for the method variables. Hence, only final keyword is permitted for such variables

Check this out for more on scope, default values and lifetime of method, instance and class variables

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.