SO i feel totally annoying but I got to ask for advice.

As you see in the title im trying to call a constructor from an other class. Im trying to add and create a new gymMember for every weight that is found in the file into a new array reading the weights of the members from that file.

We had to create a gymMember class with the properties memberid and membersweights first. Memberid since the file is filled with amount of numbers only and no strings (names). And then an other class where we create the arraylist. Im guessing we have to do it this way in order to call for a constructor in the gymMember class

I cant get it to work, i know my method for adding the members into the array is correct but the constructor is wrong somehow, its not called

i hope im being clearer this time :)

public class GymMember {

        public GymMember(double membersweight, int membersid) {

        }

    public class MemberWeight {

    ArrayList members = new ArrayList();

    public MemberWeight(){

    this.members = new ArrayList();

    }


    public void getMembers() throws FileNotFoundException{
        int membersid = 0;
        double membersweight = 0;
        try {
          Scanner input = new Scanner(new FileInputStream("C:\\prov\\vikt-fore_data2Update.csv"));
          while(input.hasNextDouble()){
          membersweight = input.nextDouble();
          members.add(new GymMember( membersweight , membersid ));
                membersid++;
                }
               } catch (FileNotFoundException ex) {
            System.out.println("");
        }

Recommended Answers

All 6 Replies

Is that all the code?
Does the GymMember class have variables for id and weight? Do you set them in the constructor? Is MemberWeight really an inner class of GymMember? Why do you initialise members when it's declared, then initialise it again in the constructor?

No there is a lot of code but i only have problem with this part . I dont have variables for memberid and memberweight in class GymMembers but do i need to have that? because i have them declared in the other class so i thought it would call the constructor that way?

WHat do you mean by : is memberWeight really an inner class of gymMember?

the initializing of member, should i not have that constructor at all?

is this correct?

public class GymMember {

        int medlemsnummer = 0;
        double medlemsvikt = 0;
        public GymMember(double medlemsvikt, int medlemsnummer) {
       this.medlemsvikt = medlemsvikt;
       this.medlemsnummer = medlemsnummer;
        }

    public class MemberWeight {

    ArrayList members = new ArrayList();



    public void getMembers() throws FileNotFoundException{
        int medlemsnummer = 0;
        double medlemsvikt = 0;
        try {
          Scanner input = new Scanner(new FileInputStream("C:\\prov\\vikt-fore_data2Update.csv"));
          while(input.hasNextDouble()){
          medlemsvikt = input.nextDouble();
          members.add(new GymMember( medlemsvikt , medlemsnummer ));
                 medlemsnummer++;
                }
               } catch (FileNotFoundException ex) {
            System.out.println("");
        }

does it work ? seems correct to me.

Yes, that's a lot better.

The variables you declare in one class are different from the ones declared in a different class, even if their names are the same. That's why you need two variables to hold the user's input and pass it to the constructor, then two more in the other class to keep those values.

My inner class question was becuase the code you posted had the second class declared before the closing bracket of the first class - meaning it looks like its decalared inside the first class. That would be valid Java syntax, but probably not what you wanted.

 } catch (FileNotFoundException ex) {
            System.out.println("");
 }

Please don't do that.
Unless you know exactly what you are doing when the code is all finished and debugged, always start with an ex.printStackTrace();in your catch blocks. Otherwize you wull never know if an Exception was thrown and will waste hours trying to find bugs when Java had aleady created a complete diagnostic for you.

Ok i understand :)
Thanks,

No it doesnt work quite yet because im having a problem with the getWeight method. Im a little confused atm but im trying to solve it. Ill tell you all in a bit :)

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.