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 5 of 5

Thread: Need help--trying to use an if/else statement that depends on which of 2 arrayLists an object is in.

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help--trying to use an if/else statement that depends on which of 2 arrayLists an object is in.

    I have two arraylists. One is personalContactList. The other is businessContactList.

    1. I want to take user input that references an object attribute (int),
    2. Use it to determine which object is referenced,
    3. Find the "type" attribute of that object,
    4. Determine which arraylist the object belongs to, based on that type,
    5. Use the if else/statement to print out some attributes that depends on which arraylist the object is in.

    I believe the code successfully does 1-3, and probably 4. But there is a hangup on 5. I get an indexoutofbounds execption. Which I'm not sure I understand very well.

    Also, I'm very new to programming, so I may be missing things that are very obvious to most of you all.

    Here is the code--it's part of a switch statement:

    case "c": {
                    //some code here prints out a list
     
                    boolean b = false;                    
                    int g;
     
                    while (b == false) {
     
                            Scanner detailScanner = new Scanner(System.in);
                            System.out.print("Enter a number to see details: ");
                            g = detailScanner.nextInt();
     
                            if (personalContactList.get(g).type.equals("personal")) {                     
                                System.out.println(personalContactList.get(g).type);
     
                            } else {                            
                                System.out.println(businessContactList.get(g).type);                            
                            } 
     
                            b = true;
                        }
                        break;

    Any suggestions?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Need help--trying to use an if/else statement that depends on which of 2 arrayLists an object is in.

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    When you're requesting help with an error message, please post the entire message, stack trace and all.

    There is a maximum number of elements in the lists. The index of the first element is 0, the index of the last element is maxIndex. If g > maxIndex, then you'll get an index out of bounds exception. You can determine the value of maxIndex using an ArrayList method. Review the ArrayList API to find out how.

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help--trying to use an if/else statement that depends on which of 2 arrayLists an object is in.

    Thanks for the info--I will read up on it.

    The exact error changes depending on how many and what type of objects are in the arraylist. For example, if I have one business contact object in the business arraylist and one personal object in the personal arraylist, the program crashes when I enter a 0 or 1. I get this:

    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    at java.util.ArrayList.rangeCheck(ArrayList.java:638)
    at java.util.ArrayList.get(ArrayList.java:414)
    at contactlistapp2.ContactListApp2.main(ContactListAp p2.java:64)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 38 seconds)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,404
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: Need help--trying to use an if/else statement that depends on which of 2 arrayLists an object is in.

    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help--trying to use an if/else statement that depends on which of 2 arrayLists an object is in.

    I think g does end up being bigger than maxIndex. I'm getting it figured out. I forgot there were two arrays when I wrote part of the code.

Similar Threads

  1. PLEASE HELP WITH JAVA ARRAYLISTS! Thanks.
    By javahelpneeded in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 5th, 2014, 04:54 PM
  2. boolean arraylists?
    By Scorks in forum Collections and Generics
    Replies: 1
    Last Post: October 15th, 2013, 11:47 AM
  3. [SOLVED] ArrayLists - Getting index of parameters of objects of ArrayLists.
    By mwebb in forum Object Oriented Programming
    Replies: 1
    Last Post: February 16th, 2012, 06:39 PM
  4. Creating ArrayLists
    By Khadafi in forum Java Theory & Questions
    Replies: 15
    Last Post: January 12th, 2012, 05:18 PM
  5. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM