-2

sorry if my question seems stupid, i'm just a newbee trying to learn java. In this code im just trying to calculate logarithm of 2 with recursive methode. But somehow eclipse tells me there are problems (the strong letters are the one which are red underlined, i tried to make those letter strong but only stars showed up instead).

Thanks and sorry for my English here. :D

import javax.swing.JOptionPane;
public class rekursivloga {
public static void main(String[] args) {
    String einlesenzahl = JOptionPane.showInputDialog("Bitte eine Zahl eingeben!");
    int zahl = Integer.parseInt(einlesenzahl);
    int ergebnis = logrekursiv(zahl);

    private int logrekursiv(int zahl) {
        if (zahl == 1) {
            return 0;
        }
        if (zahl <1 ) {
            return -1;
        }
        if (zahl >1) 
            return 1 + logrekursiv (zahl/2);
    }

}

}

2
  • And it also tells you a specific error message in the Marker view or when you hover. Commented Jul 25, 2015 at 23:16
  • Please remove the stars. Commented Jul 25, 2015 at 23:16

2 Answers 2

2

You can't have a method inside another method. Move logrekursiv into the body of the class (and make it static, since there's nothing state-based in it).

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

Comments

0

You are declaring a method (logrekursiv) inside another method (the main method). This is illegal, you should move the method oustide of the main method.

public static void main(String[] args) {
    String einlesenzahl = JOptionPane.showInputDialog("Bitte eine Zahl eingeben!");
    int zahl = Integer.parseInt(einlesenzahl);
    int ergebnis = logrekursiv(zahl);

}

private static int logrekursiv(int zahl) {
    if (zahl == 1) {
        return 0;
    }
    if (zahl <1 ) {
        return -1;
    }
    else {
        return 1 + logrekursiv (zahl/2);
   }
}

In addition, I replaced the last if-statement with an else statement, since it does exactly the same and makes the compiler sure that a value will eventually be returned.

Make sure that you read some Java tutorials, as it seems that you have yet to learn the language properly.

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.