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

Thread: For/Array Problem: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1

  1. #1
    Junior Member
    Join Date
    Feb 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default For/Array Problem: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1

    I want to add a player to a team, and delete this player from other teams, so that the player is not in 2 teams at the same time.

    I really dont know why this is giving me an error. (Error:
    HTML Code:
    java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    )

    public static void addPlayertoTeam(Player p, int teamnumber) {      
     
          print.c("Adding player to team..");
     
          if (Check.getTeamMembers(teamnumber).size() != Check.getPperTeam()) {
          print.c("Player can be added. Checking other teams..");  
           for(int i=0; i < Check.getTeamAnzahl(); i++) {
            if (Check.getTeamMembers(i+1).size() != 0) {
            print.c("Check-Team is not empty.");
             for (int j=0; j < Check.getTeamMembers(i+1).size(); j++) {
              if (Check.getTeamMembers(i+1).get(j+1) == p.getName()) {
               print.c("Player found. Removing player..");
              }
             }
            }
           }
          }
    Last edited by Norm; February 9th, 2019 at 02:45 PM. Reason: HTML tag changed to code tag

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: For/Array Problem: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1

    java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    That says the code tried to use an index of 1 (requires at least 2 items) in a list or array that only had 1 item (MAX INDEX OF 0)
    Check the code to see why it is using an invalid index.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: For/Array Problem: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1

    Quote Originally Posted by Norm View Post
    That says the code tried to use an index of 1 (requires at least 2 items) in a list or array that only had 1 item (MAX INDEX OF 0)
    Check the code to see why it is using an invalid index.
    I know, but can you tell me what exactly is wrong with my code?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: For/Array Problem: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1

    what exactly is wrong with my code?
    It is using an index that is past the end of the list.
    There aren't any comments describing what the code is trying to do so I can't point to a statement that is not doing what it is supposed to as described in the comments.

    It would help for reading the code if the indentations were more that one space. Poorly formatted code is hard to read.

    What is the index that has the value that is past the end of the list?
    Add some print statements that print out the value of the index and the length/size of the list it is being used with.
    The print out should should you where the index is wrong for the size of the list.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to re-size an array with keeping the previous value in java?
    By jisan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 30th, 2014, 01:21 AM
  2. Replies: 1
    Last Post: November 10th, 2013, 08:39 PM
  3. [SOLVED] Game problem: java.lang.IndexOutOfBoundsException with array list
    By The_pizzaguy in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 23rd, 2013, 11:30 AM
  4. Java 1.4 Array issues - controlling array size for dynamic usage
    By doonan79 in forum Collections and Generics
    Replies: 5
    Last Post: June 18th, 2013, 11:53 AM
  5. Replies: 6
    Last Post: February 27th, 2013, 10:36 AM