1

The task is to "change" the following program so that it is possible to create a subclass which enables the user to enter the numbers over Scanner:

public class Patrick3 {

    static public void main(String[] emil) throws java.io.IOException {

        System.out.println("Jetzt geht es los! Geben sie eine Zahl ein");

        while (true) {
            System.out.println("Zum Beenden bitte 0 eingeben: ");
            int n = EM.liesInt();
            if (n == 0) break;

            if (n < 0) {
                System.out.println("Die Zahl " + n + " ist zu klein!");
                continue;
            }
            BigInteger erg = new BigInteger("1");
            BigInteger faktor = new BigInteger("1");
            for (int i=1; i < n; i++) {
                faktor = faktor.add(BigInteger.ONE);
                erg = erg.multiply(faktor);
            }
            String ergString = erg.toString();
            System.out.println("Die Fakultaet von " + n + " ist gleich: ");
            System.out.println(ergString);
            int laengeD = ergString.length();
            int laengeB = erg.bitLength();
            System.out.println("Länge (in Dezimalziffern) : " + laengeD);
            System.out.println("Länge (in Binaerziffern) : " + laengeB);
        } // while
        System.out.println("Das war's erstmal!");
    }
}

I tried it like this:

public class Patrick_3 extends EM {
    static public int liesInt () throws IOException {

        System.out.println("Jetzt geht es los! Geben sie eine Zahl ein");

        while (true) {
            System.out.println("Zum Beenden bitte 0 eingeben: ");
            int n = EM.liesInt();
        }

public class EM
{
    public static void main (String [] args)
    {
        int i;
        boolean has_input_int;
        boolean isValid_int = false;
        String input = "";

        Scanner keyboard = new Scanner(System.in); //Decl. & int. a scanner.
        do {
            System.out.print("Geben Sie eine Int Zahl ein! ");

            while (!keyboard.hasNextInt()) {
                System.out.println("Fehler! Falsche Eingabe Versuchen sie es nochmals!");
                keyboard.next();
            }
            i = keyboard.nextInt();
            isValid_int = true;
        } while (isValid_int == false);
    }
}

But it says

cannot find symbol - method liesint()

Where is the problem?

Pang
  • 9,564
  • 146
  • 81
  • 122
Phoney
  • 21
  • 3

2 Answers2

0

Because you have to call liesInt() without this EM. You're never creating an EM object you're extending it into your class.

SeTirap
  • 11
  • 4
0

Add following method in your EM class or simply modify main method to liesInt as follows:

public static int liesInt() {
    int i;
    boolean has_input_int;
    boolean isValid_int = false;
    String input = "";

    Scanner keyboard = new Scanner(System.in); // Decl. & int. a scanner.
    do {
        System.out.print("Geben Sie eine Int Zahl ein! ");

        while (!keyboard.hasNextInt()) {
            System.out.println("Fehler! Falsche Eingabe Versuchen sie es 
nochmals!");
            keyboard.next();
        }
        i = keyboard.nextInt();
        isValid_int = true;
    } while (isValid_int == false);
    return i;
}
kiranbirajdar
  • 195
  • 2
  • 12