0

I hope you can help me with this one.

I wrote a Code to create a databank. For that I created a method that uses the Scanner to store a userinput and in the end a simple println to show what the input was. That all works fine.

However, when I want to execute the method a second time, I´m not beeing asked anymore to give another input. It´s only the same input that was given before printed again.

My Code contains the Method and the print line:

datenErfassen();    
System.out.println(g + " " + t + " " + n + " " + v + " " + s + " " + h + " " + p + " " + o);
datenErfassen();    
System.out.println(g + " " + t + " " + n + " " + v + " " + s + " " + h + " " + p + " " + o);

And that´s the Output:

Gib dein Geschlecht ein. (weiblich/männlich)
weiblch
Gib den Titel ein.
Student
Gib den Nachnamen ein.
Müller
Gib den Vornamen ein.
Lisa
Gib die Strasse ein.
Rosenstr
Gib deine Hausnummer ein.
34
Gib deine Postleizahl ein.
76543
Gib den Ort ein.
München
weiblch Student Müller Lisa Rosenstr 34 76543 München
weiblch Student Müller Lisa Rosenstr 34 76543 München

The second method is just being ignored. Why can´t I type in another persons data? My guess is, that it has something to do with the boolean inputGeschlechtError I created within the method. I did that, so i can loop the code to ask for another input, if the data not meeting the guidelines like limited characters:

while (inputGeschlechtError) {
    try {
        System.out.println("Gib dein Geschlecht ein. (weiblich/männlich)");
        eingabeGeschlecht = new Scanner(System.in);
        g = eingabeGeschlecht.nextLine();
        inputGeschlechtError = (String.valueOf(g).length() > 8);
    } catch (NumberFormatException e) {
        inputGeschlechtError = true;
    }
    if (inputGeschlechtError) {
        System.out.println(
        "Fehler: Die Eingabe darf maximal 8 Zeichen haben. Bitte gib `männlich` oder `weiblich` ein.");
    }
}

I tried to fix the Problem setting the boolean to false and placing it at any possible position in the code and I tried using the scanner.reset() method, but that didn´t change anything.

Please tell me, if there already is an answer to that, I didn´t find one yet.

Edit:

Here´s some more of my code for better understanding. The datenErfassen method contains of one method per question. These all have the same construcion:

public static void datenErfassen(){
    geschlechtErfassen(g);
    titelErfassen(t);
    nameErfassen(n);
    vornameErfassen(v);
    strasseErfassen(s);
    hausnummerErfassen(h);
    plzErfassen(p);
    ortErfassen(o);
    
}

public static void geschlechtErfassen(String geschlecht) {
    while (inputGeschlechtError) {
        try {
            System.out.println("Gib dein Geschlecht ein. (weiblich/männlich)");
            eingabeGeschlecht = new Scanner(System.in);
            g = eingabeGeschlecht.nextLine();
                inputGeschlechtError = (String.valueOf(g).length() > 8);
        } catch (NumberFormatException e) {
            inputGeschlechtError = true;
        }
        if (inputGeschlechtError) {
            System.out.println(
            "Fehler: Die Eingabe darf maximal 8 Zeichen haben. Bitte gib `männlich` oder `weiblich` ein.");
        }
    }
}

public static void titelErfassen(String titel) {
    while (inputTitelError) {
        try {
            System.out.println("Gib den Titel ein.");
            Scanner eingabeTitel = new Scanner(System.in);
            t = eingabeTitel.nextLine();
            inputTitelError = (String.valueOf(t).length() > 10);
        } catch (NumberFormatException e) {
            inputTitelError = true;
        }
        if (inputTitelError) {
            System.out.println("Fehler: Die Eingabe darf maximal 10 Zeichen haben.");
        }
    }
}

public static void nameErfassen(String name) {
    while (inputNameError) {
        try {
            System.out.println("Gib den Nachnamen ein.");
            Scanner eingabeName = new Scanner(System.in);
            n = eingabeName.nextLine();
            inputNameError = (String.valueOf(n).length() > 25);
        } catch (NumberFormatException e) {
            inputNameError = true;
        }
        if (inputNameError) {
            System.out.println("Fehler: Die Eingabe darf maximal 25 Zeichen haben.");
        }
    }
}

public static void vornameErfassen(String vorname) {
    while (inputVornameError) {
        try {
            System.out.println("Gib den Vornamen ein.");
            Scanner eingabeVorname = new Scanner(System.in);
            v = eingabeVorname.nextLine();
            inputVornameError = (String.valueOf(v).length() > 25);
        } catch (NumberFormatException e) {
            inputVornameError = true;
        }
        if (inputVornameError) {
            System.out.println("Fehler: Die Eingabe darf maximal 25 Zeichen haben.");
        }
    }
}

public static void strasseErfassen(String strasse) {
    while (inputStrasseError) {
        try {
            System.out.println("Gib die Strasse ein.");
            Scanner eingabeStrasse = new Scanner(System.in);
            s = eingabeStrasse.nextLine();
            inputStrasseError = (String.valueOf(s).length() > 25);
        } catch (NumberFormatException e) {
            inputStrasseError = true;
        }
        if (inputStrasseError) {
            System.out.println("Fehler: Die Eingabe darf maximal 25 Zeichen haben.");
        }
    }
}

public static void hausnummerErfassen(String hausnummer) {
    while (inputHausnummerError) {
        try {
            System.out.println("Gib deine Hausnummer ein.");
            Scanner eingabeHausnummer = new Scanner(System.in);
            h = eingabeHausnummer.nextLine();
            inputHausnummerError = (String.valueOf(h).length() > 5);
        } catch (NumberFormatException e) {
            inputHausnummerError = true;
        }
        if (inputHausnummerError) {
            System.out.println("Fehler: Die Eingabe darf maximal 5 Zeichen haben.");
        }
    }
}

public static void plzErfassen(int plz) {
    while (inputPlzError) {
        try {
            System.out.println("Gib deine Postleizahl ein.");
            Scanner eingabePlz = new Scanner(System.in);
            try {
                p = eingabePlz.nextInt();
            } catch (Exception e) {
                System.out.println("Die Eingabe ist ungültig. Bitte gib eine Zahl an." + e.toString());
                System.out.println("Gib deine Postleizahl ein.");                                       
                Scanner eingabePlz2 = new Scanner(System.in);
                p = eingabePlz.nextInt();
            }
            inputPlzError = (String.valueOf(p).length() > 5);
        } catch (NumberFormatException e) {
            inputPlzError = true;
        }
        if (inputPlzError) {
            System.out.println("Fehler: Die Eingabe darf maximal 5 Zeichen haben.");
        }
    }
}

public static void ortErfassen(String ort) {
    while (inputOrtError) {
        try {
            System.out.println("Gib den Ort ein.");
            Scanner eingabeOrt = new Scanner(System.in);
            o = eingabeOrt.nextLine();
            inputOrtError = (String.valueOf(o).length() > 25);
        } catch (NumberFormatException e) {
            inputOrtError = true;
        }
        if (inputOrtError) {
            System.out.println("Fehler: Die Eingabe darf maximal 25 Zeichen haben.");
        }
    }
}
  • I see that you are creating a new scanner on every loop iteration. It would probably be better to do it just once above the loop. But to see the actual problem we will need a bit more of the `datenErfassen` method. – TiiJ7 Dec 16 '20 at 16:04
  • @TiiJ7 Thanks for the reply, posted the code now. How can I ask for different userinputs with only one Scanner? – vivianejohns Dec 16 '20 at 16:13
  • Hmm, you have a lot of code duplication, there is definitely a lot you can optimize. But as for your actual problem, I think you never put your booleans back to `true` (not false) when you re-rerun the method. You can change all those booleans to local variables, aka put `boolean inputGeschlechtError = true;` as the first line in `geschlechtErfassen`, etc. Otherwise they will remain false from the last time and the loop will be skipped. – TiiJ7 Dec 16 '20 at 16:34
  • @TiiJ7 THANK YOU! That worked (: And thank you also for the tip that I can optimize my Code, I´ll do that next. You saved my day. – vivianejohns Dec 16 '20 at 16:45
  • Please make your posts all-English, it helps a lot to gain reopen votes. – peterh Dec 18 '20 at 11:35
  • @peterh-ReinstateMonica thank you for the tip. For this question I already got an answer, but I´ll think of it, when I have another question. – vivianejohns Dec 18 '20 at 16:29

0 Answers0