Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Finding a String value in the ArrayList

  1. #1
    Junior Member
    Join Date
    Jan 2022
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Question Finding a String value in the ArrayList

    Hello again,

    How can I find a String value in the class Person, and add it to an already exisitng piece of coding?

    For the assignment, I need to check if a last name is already in the ArrayList. If it is, the person cannot enter, if not the person can enter and then needs to follow the piece of coding in the second piece of code attached below.

    Below is the code I could find in my textbook, but I get an IndexOutOfBoundsException error. What I'm a missing here, that it is not working?

    Thanks for the help!

    Code to find if there is already a last name in the arraylist
        public String searchLastName (Person name)
        {
            int index = 0;
     
            while (index < row1.size() || index < row2.size() && row1.get(index).getLastName() == name.getLastName() || row2.get(index).getLastName() == name.getLastName())
            {
                index++;
            }
     
            return index;
        }

    Code to determine in which row the person needs to stand.
        public void enterBakkery(Person name)
        {
            if(name.hasVIPpass() == true)
            {        
                if(construction == true)
                {
                    if(maxInRow1 < 15)
                    {
                        maxInRow1++;
                        row1.add(name);
                    }
                    else
                    {
                        row2.add(name);
                    }
                }
                else
                {
                    if(maxInRow1 < 10)
                    {
                        maxInRow1++;
                        row1.add(name);
                    }
                    else
                    {
                        row2.add(name);
                    }
                }
            }
     
            else 
            {
                row2.add(name);
            }
        }

  2. #2
    Member
    Join Date
    Apr 2022
    Posts
    36
    Thanks
    0
    Thanked 8 Times in 8 Posts

    Default Re: Finding a String value in the ArrayList

        public int searchRow1(Person name) {
            if(row1.size() > 0) {
                for (int i = 0; i < row1.size(); i++) {
                    if (row1.get(i).getName().equals(name.getName())) {
                        return i;
                    }
                }
            }
            return -1;
        }
        public int searchRow2(Person name) {
            if(row2.size() > 0) {
                for (int i = 0; i < row2.size(); i++) {
                    if (row2.get(i).getName().equals(name.getName())) {
                        return i;
                    }
                }
            }
            return -1;
        }
        public int searchLastName(Person name) {
            int index1 = searchRow1(name), index2 = searchRow2(name);
            return index2 > -1 ? index2 :  index1;
        }

  3. The Following User Says Thank You to andreaita44 For This Useful Post:

    F_VRBRG (May 3rd, 2022)

Similar Threads

  1. Replies: 2
    Last Post: March 28th, 2013, 09:54 AM
  2. Finding String in ArrayList
    By Noob_Programmer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 6th, 2011, 05:18 AM
  3. private Map<String, ArrayList> xlist = new HashMap<String, ArrayList>();
    By Scotty in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 21st, 2011, 08:37 AM
  4. Help with Finding Max and Min Values of ArrayList
    By CheekySpoon in forum Collections and Generics
    Replies: 3
    Last Post: March 2nd, 2011, 08:57 PM
  5. [SOLVED] String Matcher finding only char not a whole string
    By Kakashi in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 18th, 2011, 09:58 AM