Since Input of ENTITY and of it's attribute is ok
I am trying to op the value of the entity and it's attribute details

so my modified code abt ENTITY is:

package datamodel;

import java.io.*;
import java.util.*;

public class ENTITY{

    String entity_name;
    int no_of_attribute;
    int no_of_entity;
    attribute attr1;

    public ENTITY(String ename,int nofattr,int nofentity)throws IOException
    {
            entity_name=ename;
            no_of_attribute=nofattr;
            no_of_entity=nofentity;

    }


    attribute[] attr=new attribute[no_of_attribute];


    public void setAttributeValues()throws IOException,java.lang.ArrayIndexOutOfBoundsException
    {

        System.out.println("We are prompting for entering data....");

        for(int i=0;i<no_of_attribute;i++)
        {
          attr[i]=new attribute();
          BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
          System.out.print("Enter the name of the attribute=");
          attr[i].name=br.readLine();

          System.out.print("Enter the key of the attribute=");
          attr[i].key=br.readLine();

          System.out.print("Enter the datatype of the attribute=");
          attr[i].datatype= br.readLine();

          System.out.print("Enter the not_null property (y/n) of the attribute=");
          if(br.readLine().equals("Y") || br.readLine().equals("y"))
          {
            attr[i].not_nul=true;
          }
          else
          {
                attr[i].not_nul=false;
          }

          System.out.print("Enter the default value of the attribute=");    
          attr[i].default_value=br.readLine();

          System.out.print("Enter the check for the attribute=");
          attr[i].check=br.readLine();

            System.out.print("Enter the uniqueness(y/n) of the attribute=");
            if(br.readLine().equals("Y") || br.readLine().equals("Y"))
            {
            attr[i].unique=true;
            }
            else
            {
                attr[i].unique=false;

            }

            System.out.print("Enter the comment of the attribute=");
            attr[i].comment=br.readLine();

            System.out.print("Enter the multivalue(y/n) of the attribute=");
            if(br.readLine().equals("Y") || br.readLine().equals("Y"))
            {
            attr[i].multivalue=true;
            }
            else
            {
                attr[i].multivalue=false;

            }

        }

 }

  public void getAttributeValues(){

    for(int i=0;i<no_of_entity;i++){
        System.out.println("Name Of Entity="+this.entity_name);
        for(int j=0;j<no_of_attribute;j++){
            System.out.println("The name of the attribute="+this.attr[i].name);
            System.out.println("The key of the attribute="+this.attr[i].key);
            System.out.println("The datatype of the attribute="+this.attr[i].datatype);
            System.out.println("The not_null property (y/n) of the attribute="+this.attr[i].not_nul);
            System.out.println("The default value of the attribute="+this.attr[i].default_value);
            System.out.println("The check for the attribute="+this.attr[i].check);
            System.out.println("The uniqueness(y/n) of the attribute="+this.attr[i].unique);
            System.out.println("The comment of the attribute="+this.attr[i].comment);
            System.out.println("The multivalue(y/n) of the attribute="+this.attr[i].multivalue);
        }
    }
  }
}

where bold is function which op the value stored into the entity

to call the function I have written another change in the erd.java (where the main class remains)code:

package datamodel;
import java.io.*;
import java.util.*;

public class erd {

    public static void main(String args[])throws IOException{
        String ename;
        int nofentity;
        int nofattr;
        System.out.print("Enter no. of entities=");
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        nofentity=Integer.parseInt(br.readLine());
        ENTITY[] e=new ENTITY[nofentity];

        for(int i=0;i<nofentity;i++)
       {
            System.out.print("Enter name of entity=");
            ename=br.readLine();
            System.out.print("Enter no. of attributes=");
            nofattr=Integer.parseInt(br.readLine());
            e[i]=new ENTITY(ename,nofattr,nofentity);
            e[i].setAttributeValues();
            [B]e[i].getAttributeValues();[/B]
      }

    }



}

My problem is when I put the line: attribute[] attr=new attribute[no_of_attribute]; inside of the function public void setAttributeValues() in the class ENTITY a lot of compilation error is detected because getArrtibuteValues() does not get the array (because the array is local member of the fn setAttributeValues()) but after omitting the the function setAttributeValues() all input are inputted smoothly. When I put the line attribute[] attr=new attribute[no_of_attribute]; outside any function(as shown in this code) then ArrayIndexOutOfBoundException shown in line: attr[i].name=br.readLine(); when I input the values of the arribute of the entity. throwing of the exception doesnot result any fruitful

plz suggest me something what modification I should do in our code of ENTITY class to avoid such exception

Recommended Answers

All 6 Replies

Oh gosh, that's a large post! First, I don't think you require throws IOException in the constructor of ENTITY. Second, please use the Java convention for identifier names, it's your friend honest :icon_mrgreen: Instead of ENTITY it should be Entity.

At first glance, I think your error is because of: attribute[] attr=new attribute[no_of_attribute]; though I'm actually a bit in conflict about that. So try putting attr = new attribute[no_of_attribute]; within your constructor. And simply have attribute attr; declared as a field variable. I'm not sure how good your Java is but if you don't understand what I've said, please let me know. Moveover, I'm not sure if this will resolve your problem but it's easier to test that out than for me to look at all of your code. Oh and please use the DaniWeb tags when quoting programming code :icon_wink:

Oh and it should be Attribute and not attribute as per the Java naming convention :icon_biggrin:

One last thing, never iterate over an array like this: for(int i=0;j<no_of_attribute;j++) If you change it to: for(int i=0;j<attr.length;j++) then you will stop getting that Exception

I just noticed, you shouldn't have if(br.readLine().equals("Y") || br.readLine().equals("y")) as you're requesting data from the input stream twice if the user actually presses a y the first time. Instead use if(br.readLine().equalsIgnoreCase("Y"))

attribute[] attr=new attribute[no_of_attribute];

attr has no_of_attribute arguments

for(int i=0;i<no_of_entity;i++){

i goes from 0 to no_of_entity

for(int j=0;j<no_of_attribute;j++){

j goes from 0 to no_of_attribute

j is never used

System.out.println("The name of the attribute="+this.attr[i].name);

you access attr

but i might be bigger than the size of attr, which is no_of_attribute
because no_of_entity might be bigger than no_of_attribute

commented: Good one on the debug! +2

hehe Indeed he does have his indices mixed up! But I would like to check up on the instantiation of the array outside the constructor. I think that the new value of no_of_attributes would be updated before the array is created, but I'm not sure. I'll check it out later. Hope that solves his problem though.

Oh gosh, that's a large post! First, I don't think you require throws IOException in the constructor of ENTITY. Second, please use the Java convention for identifier names, it's your friend honest :icon_mrgreen: Instead of ENTITY it should be Entity.

At first glance, I think your error is because of: attribute[] attr=new attribute[no_of_attribute]; though I'm actually a bit in conflict about that. So try putting attr = new attribute[no_of_attribute]; within your constructor. And simply have attribute attr; declared as a field variable. I'm not sure how good your Java is but if you don't understand what I've said, please let me know. Moveover, I'm not sure if this will resolve your problem but it's easier to test that out than for me to look at all of your code. Oh and please use the DaniWeb tags when quoting programming code :icon_wink:

Oh and it should be Attribute and not attribute as per the Java naming convention :icon_biggrin:

One last thing, never iterate over an array like this: for(int i=0;j<no_of_attribute;j++) If you change it to: for(int i=0;j<attr.length;j++) then you will stop getting that Exception

Thanx your suggestion helped me to solve the problem declaring variable at the first
and initializing it into the constructor solved the problem
PS Sorry for late reply

That is rather interesting... ah such is life that a new day brings new knowledge. P.S. don't forget to mark your post as solved :icon_wink:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.