2

I'm trying to get from Node of class String but the compiler cannot find the Variable. I would appreciate your help.

Student class:

public class Students {

    private int[] marks;
    private String name;

    public Students(String name, int[] marks) {
        this.name = name;
        marks = new int[5];
        for (int i = 0; i < marks.length; i++) {
            this.marks[i] = marks[i];
        }
    }
    public int getMarksI(int i) {
        return marks[0];
    }
    public int[] getMarks() {
        return marks;
    }
    public String getName() {
        return name;
    }
}

The function where the problem:

public double AverageByName(Node < Students > s, String name) {
    Node < Students > p = s;
    while (p != null) {
        if (name == p.getName()) // this is the problem "cannot find symbol p.getName"
        {
            //
        }
        p.setNext();
    }
    return false;
}

In addition to that there is a class of Node and the linked list of the Students class created without a problem in the main.

Parkash Kumar
  • 4,710
  • 3
  • 23
  • 39
holik9
  • 31
  • 4
  • 3
    despite your actual problem you should have a look at [how do i compare strings in java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – SomeJavaGuy Oct 21 '15 at 13:51
  • 2
    Look at the type of `p` - it's a `Node`, not `Student`. Now you haven't shown us the `Node` class, but presumably it's not got a `getName()` method... – Jon Skeet Oct 21 '15 at 13:51

2 Answers2

1

If Node exposes its Students value with getValue() it would be:

if (name.equals(p.getValue().getName())) 
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
1

It should be something like

public double AverageByName(Node < Students > s, String name) {
Node < Students > p = s;
while (p != null) {
    if (name.equals(p.getValue().getName())) // you need to get the Student object from the node first
    {
        //
    }
    p.setNext();
}
return false;

}

or may be cast it to

Student std = (Student)p.getValue();

I have no idea from your question about the Node class. It would be better to share Node class also.

awsome
  • 2,143
  • 2
  • 23
  • 41