Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9130449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:58:05+00:00 2026-06-17T07:58:05+00:00

I am successfully adding the first student but when i add a second one

  • 0

I am successfully adding the first student but when i add a second one i get
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 11

at java.util.Vector.get(Unknown Source)  
    at business.StudentCollection.UseArray(StudentCollection.java:58  
    at business.Application.main(Application.java:30) 

Segments of code

 public class StudentCollection {  
private Vector<Student> collection;  
private int count;  

public StudentCollection ()
{  
collection=new Vector<Student>(10,2);  
count=0;  
for( int i=0;i< collection.capacity(); i++) //i read that object cannot be added to 
vectors if empty  
collection.add(i,new Student(0,"No Student",0));

}  

public void addStud(int ID,String name,int Credits)
   {    

for(int i=0;i< collection.capacity();i++)  
 if(collection.get(i)==null)  // new Error
collection.add(i,new Student(0,"No Student",0)); //making sure vector new index are   filled

collection.add(count,new Student(ID,name,Credits));  
count++;  

  }  
public Student UseArray(int x){  \\ Error here line 58
return collection.get(x);  

                      }

 public int getlengthh(){  
    return collection.capacity();  
                }  
}  
 public static void main (String [] args ){  
 StudentCollection C=new StudentCollection();  


        System.out.println("Enter Student's ID");  
        x=scan.nextInt();  
        for (int i=0;i< C.getlengthh();i++){    
if(C.UseArray(i).getID()==x){  // Error here
        System.out.println("A student with this ID already exists.Do you want to overwrite the existing student?yes/no");  
        scan.nextLine();  
        ans=scan.nextLine();  

        if (ans.equalsIgnoreCase("yes")){
            C.delete(x);
        continue;
        }
        else {
            System.out.println("Enter Student's ID");
        x=scan.nextInt();
        }
            }
        }

        System.out.println("Enter Student's name");
        Str=scan.nextLine();
        Str=scan.nextLine()+Str;
        System.out.println("Enter number of credits");
        y=scan.nextInt();
        C.addStud(x,Str,y);

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T07:58:06+00:00Added an answer on June 17, 2026 at 7:58 am

    Modify to

     public Student UseArray(int x){  \\ Error here line 58
         if(collection.size() > x)
            return collection.get(x); 
         return null; 
    
        }
    

    There is a difference between capacity and size. Capacity returns length of array created by Vector to hold the present and the incoming elements. While size is the number of elements already put into the vector. Having said that, While checking for existence of elements don’t use capacity use size as below:

     public int getlengthh(){  
        return collection.size();  
                    } 
    

    Even if capacity is bigger than index still add can throw exception. See here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Been succcesfully adding records using Linq2Sql with the linq2sql genereated classes, works great.. But
Successfully used jquery-infinite-carousel in the past but for my current project I need it
I successfully compiled the following program simple.cc: #include <gtkmm.h> int main (int argc, char
After successfully opening Pop3Folder , and retrieving messages from it, I then sometimes get
After successfully acquiring an oauth_token for a user, I am able to get successful
I've successfully set up a Tab Bar controller with. Table View in the first
Trying to use Database Designer in VS2010 for the first time. I started adding
I am creating tables and column dynamically. First creating tables and then adding columns
Basically: I am adding four buttons to a java form. The buttons will enable
I need to add and subtract using Javascript. I've been successful at adding all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.