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.

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

1 Answer 1

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();
}
Sign up to request clarification or add additional context in comments.

4 Comments

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 stackoverflow.com/questions/10976212/… 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.
mitgliederliste is your Collection of AddMitglied, add it to the isValid as an argument
I updated the answer. If you use diamond notation it might be ArrayList<AddMitglied> instead for the argument type.
Thank you nevabyte,.. I had to google and figure certain things out but now it works just perfectly fine :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.