0

I have a problem that I now tried to solve for about a week, but did not get nearly anywhere. Therefore I decided to ask "mighty" Stackoverflow. Sorry if this seems trivial to experienced JAVA coders.

I have an ArrayList object which extends a custom class. My class is called "AddMitglied" and is meant to store members inside my program. User will add new members and they are stored inside an ArrayList.

package mitgliederverwaltung;

public class AddMitglied {

  public String addLastName;
  public int addmemberNumber;
  public String addFirstName;
  public String adddateobBirth;
  public String addstreet;
  public int addNumber;
  public int addPlz;
  public String addtown;
  public String addEmailadresse;
  public int addTelefonnumber;

    AddMitglied(String addLastName,int addmemberNumber,String addFirstName,
                String adddateobBirth,String addstreet,int addNumber,
                int addPlz,String addtown,String addEmailadresse,
                int addTelefonnumber)
    {
        this.addLastName = addLastName;
        this.addmemberNumber = addmemberNumber;
        this.addFirstName = addFirstName;
        this.adddateobBirth = adddateobBirth;
        this.addstreet = addstreet;
        this.addNumber = addNumber;
        this.addPlz = addPlz;
        this.addtown = addtown;
        this.addEmailadresse = addEmailadresse;
        this. addTelefonnumber = addTelefonnumber;
    }  
}

Obviously I want to grab some kind of member number (public int addNumber) to be able to get each member via

mitgliederliste.get(i).addNumber

so far so good,.. I can add members. As well I want to check if a member number already exists when user enters a new member. To check if an "int" is entered into the program I have written the following method:

public static int validateinputUser(){

        System.out.println("give a number (only numeric values allowed): ");  
        Scanner inputUser = new Scanner(System.in);
            int inputnumber = 0;

                while (!inputUser.hasNextInt()) 
                    {
                        inputUser.next();
                        System.out.println("please enter numeric value");
                    }
                inputnumber = inputUser.nextInt();
                return 0;
        }
    }

So far this also works fine.. Now my problem is how to check if user input is a number & check if the number already exists in my Arraylist to avoid multiply users with the same user number... I googled heavily but somehow did not get anywhere near a suitable solution.

What I did found here is the following question: Check if a value exists in ArrayList . Over there I learned that there is a so called "contains" method. But there he already knew which one to use inside contains. I cannot use "contains" method because I somehow have to loop through all members and check if one already has the member number. If so user has to stay inside a loop to force him to choose another one.

I have found a site on the web that tells me how to do this:http://crunchify.com/how-to-iterate-through-java-list-4-way-to-iterate-through-loop/ but there he iterates over an ArrayList with strings which is easy but not directly to the point of my problem. A method mentioned there is

java.util.stream.Stream.forEach

but no idea how to use this in my concrete example.

Community
  • 1
  • 1
  • " I cannot use "contains" method because I somehow have to loop" why? This part of you question is not clear –  Apr 26 '17 at 20:38

1 Answers1

1

You want users to only be aloud inside the mitgliederliste if a current user is not already using the addNumber, if I understand correctly.

You need to check if the addNumber that the user chooses does not relate to a current user in the mitgliederliste.

public static boolean isValid(int inputnumber, ArrayList mitgliederliste) {
  for (AddMitglied a : mitgliederliste) {

     if (a.addNumber == inputnumber) {

      return false;
     }
  }
 return true;

}

To include it with your while loop;

int inputnumber = 0;

while (inputnumber == 0 || !isValid(inputnumber)) {

  while (!inputUser.hasNextInt()) {                        
      inputUser.next();
      System.out.println("please enter numeric value");
  }
  inputnumber = inputUser.nextInt();
}
  • Hi thank you very much, this is what I meant. But somehow I get scoping problems when implementing those methods in my code. NetBeans asks me to define a local variable "mitgliederliste". I have found this http://stackoverflow.com/questions/10976212/arraylist-as-global-variable but not sure how to do this. I found nothing on google to make an ArrayList global so that all methods inside my class can access it directly. – BayerischerSchweitzer Apr 27 '17 at 19:11
  • `mitgliederliste` is your Collection of `AddMitglied`, add it to the `isValid` as an argument –  Apr 27 '17 at 19:14
  • I updated the answer. If you use diamond notation it might be `ArrayList` instead for the argument type. –  Apr 27 '17 at 19:28
  • Thank you nevabyte,.. I had to google and figure certain things out but now it works just perfectly fine :-) – BayerischerSchweitzer May 03 '17 at 19:24