vendredi 28 février 2020

When I for loop through an ArrayList with If condicions the items before the searched ones get displayed

I wanted to create a program which is Object Oriented, that consists of students stored in an ArrayList in the Grade class. Every Student has a name, a surname and an age. So what I wanted to accomplish is getting back all the information of one student in the ArrayList in the console by using a Scanner. So I basically store the user given value and compare it in an if-Statement with the names or surnames in the ArrayList using a for-loop. If the passed name doesn't equal one of the names in the ArrayList, a message says the person does not exist in this grade.

It works but if I loop through the ArrayList to get person on index 2 for instance, the message that the person doesn't exist in this grade pops up for index 0 and 1 and I dont want that.

I'm from Germany, thats why most of the stuff is written in German. This is the code: ` package package_Fachklassen;

import java.util.Scanner;

public class Startklasse {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Schulklasse sk1, sk2;
    sk1 = new Schulklasse("WG 13-1");
    sk2 = new Schulklasse("WG 13-2");

    //Klasse 13-1

    Schueler s1, s2, s3, s4, s5, s6, s7, s8, s9, s10;
    s1 = new Schueler("Gonzalo", "Cofre Real", 18, sk1);
    s2 = new Schueler("Swaastika", "Yogeswaran", 20, sk1);
    s3 = new Schueler("Derin", "Sipahi", 19, sk1);
    s4 = new Schueler("Henry", "Reinhold", 19, sk1);
    s5 = new Schueler("Sara", "Perkovic", 19, sk1);
    s6 = new Schueler("Honia", "Aref", 19, sk1);
    s7 = new Schueler("Finn", "Bolheimer", 17, sk1);
    s8 = new Schueler("Karoline", "Garkos", 18, sk1);
    s9 = new Schueler("Hannah", "Hirth", 18, sk1);
    s10 = new Schueler("Sarah", "Schwarz", 18, sk1);

    sk1.addSchueler(s1);
    sk1.addSchueler(s2);
    sk1.addSchueler(s3);
    sk1.addSchueler(s4);
    sk1.addSchueler(s5);
    sk1.addSchueler(s6);
    sk1.addSchueler(s7);
    sk1.addSchueler(s8);
    sk1.addSchueler(s9);
    sk1.addSchueler(s10);

    //Klasse 13-2

    Schueler ss1, ss2, ss3, ss4, ss5, ss6;
    ss1 = new Schueler("Cem", "Yildiz", 19, sk2);
    ss2 = new Schueler("Fathima", "Baba", 19, sk2);
    ss3 = new Schueler("Sanjie", "Bislimi", 19, sk2);
    ss4 = new Schueler("Jasmine", "Jaffari", 19, sk2);
    ss5 = new Schueler("Hannah", "Arent", 19, sk2);
    ss6 = new Schueler("Niklas", "Genf", 18, sk2);

    sk2.addSchueler(ss1);
    sk2.addSchueler(ss2);
    sk2.addSchueler(ss3);
    sk2.addSchueler(ss4);
    sk2.addSchueler(ss5);
    sk2.addSchueler(ss6);



    while(true) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Welche Klasse besucht der gesuchte Schüler?");
        String k_input= scan.nextLine();
        if(k_input.equals(sk1.getBezeichnung())) {
            System.out.println("Welchen SChüler aus Klasse 13-1 suchen Sie?");
            String n_input = scan.nextLine();
            for(int i = 0; i < sk1.getListenLaenge(); i++) {
                if(n_input.equals(sk1.getSchueler(i).getName()) || n_input.equals(sk1.getSchueler(i).getNachname())) {
                    System.out.println("Ihr Schüler:");
                    System.out.println(" " + sk1.getSchueler(i).getName());
                    System.out.println(" " + sk1.getSchueler(i).getNachname());
                    System.out.println(" " + sk1.getSchueler(i).getAlter());
                    break;
                }
                else if(){
                    System.out.println("Schüler existiert nicht oder ist nicht in dieser Klasse!");
                }
            }
        }
    }

} }

  • Welche Klasse besucht der gesuchte Schüler?
  • WG 13-1
  • Welchen Schüler suchen Sie?
  • Derin
  • Schüler existiert nicht oder ist nicht in dieser Klasse!
  • Schüler existiert nicht oder ist nicht in dieser Klasse!
  • Ihr Schüler:
  • Derin
  • Sipahi
  • 19
  • Welche Klasse besucht der Schüler?

Aucun commentaire:

Enregistrer un commentaire