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.