3
class Buffer{
  public String name;
  public int id;
}

public class Main {
    public static void main(String[] args) {
      Vector<Buffer> vec = new Vector<Buffer>();
      getName = G.sIn.ReadInputLine();
      getID = G.sIn.ReadInt();
      vec.name.add(getName); // error
      vec.id.add( getID );  // error

    }
}

I'm new to Java.

However, I want to save two variable into name and id of vector and keep going.

Now, I don't know how to correctly implemenet.

Itati
  • 193
  • 11
  • vec is a vector which contains Buffer instances, it isn't a Buffer instance, yet you try to treat it as one, that's why it doesn't work. – Stultuske Jun 19 '20 at 08:14
  • @Stultuske so I missing Buffer instances? It can't use two variable to save? – Itati Jun 19 '20 at 08:19

3 Answers3

1

vec itself is not an instance of Buffer class, so you can't access the data members or methods of Buffer class on vec.

vec is a list which can contain instances of Buffer class.

Vector<Buffer> vec = new Vector<Buffer>();
getName = G.sIn.ReadInputLine();
getID = G.sIn.ReadInt();

// create an instance of Buffer class
Buffer buff = new Buffer();
buff.name = getName;
buff.id = getID;

// add the instance of Buffer class in vec
vec.add(buff);

However, its not a good practice to have public data members inside a class. All data members of Buffer class should be private and should be initialized using the constructor.

class Buffer{
  private String name;
  private int id;

  public Buffer(String name, int id) {
      this.name = name;
      this.id = id;
  }
}

public class Main {
    public static void main(String[] args) {
        Vector<Buffer> vec = new Vector<Buffer>();
        getName = G.sIn.ReadInputLine();
        getID = G.sIn.ReadInt();

        // create an instance of Buffer class
        Buffer buff = new Buffer(getName, getID);

        // add the instance of Buffer class in vec
        vec.add(buff);
    }
}

To access or set the value of a private data member of a class, you need to define Getter and Setter functions in Buffer class.

Yousaf
  • 27,861
  • 6
  • 44
  • 69
1

A vector is just like an ordered list, similar to arrayList. In your implentation, it doesn't have a name or id property. It's your Buffer class that has them. And you declared it as a vector of Buffers. So this would be more correct:

class Buffer{
  public String name;
  public int id;
}

public class Main {
    public static void main(String[] args) {
      Vector<Buffer> vec = new Vector<Buffer>();
      getName = G.sIn.ReadInputLine();
      getID = G.sIn.ReadInt();
      Buffer buf = new Buffer();
      buf.name = getName;
      buf.id = getID;
      vec.add(buf);
    }
}

It would be quicker if your Buffer class had a constructor, then you could create it without need of a buf variable with vec.add(new Buffer(getID, getName));

Kaddath
  • 5,933
  • 1
  • 9
  • 23
1
class Buffer{
  public String name;
  public int id;
}

public class Main {
    public static void main(String[] args) {
      Vector<Buffer> vec = new Vector<Buffer>();
      getName = G.sIn.ReadInputLine();
      getID = G.sIn.ReadInt();
      vec.name.add(getName); // error this can't work. vec isn't a Buffer
      vec.id.add( getID );  // error same problem    
    }
}

You need to store instances of Buffer, not their variables:

public class Main {
    public static void main(String[] args) {
      Vector<Buffer> vec = new Vector<Buffer>();
      Buffer tmp = new Buffer();
      vec.add(tmp); // this will work, you add a Buffer, but it will have default (0's) for the values
    }
}

A reworked version, using your input:

public class Main {
    public static void main(String[] args) {
      Vector<Buffer> vec = new Vector<Buffer>();
      Buffer tmp = new Buffer();
      tmp.name = G.sIn.ReadInputLine();
      tmp.id = G.sIn.ReadInt();
      vec.add(tmp); // this will work, you add a Buffer, but it will have default (0's) for the values
    }
}

I would recommend, though, to read up on naming conventions.

Stultuske
  • 9,296
  • 1
  • 25
  • 37