Question

This is my code so far:

public class PersonTester
{
 public static void main(String[] args)
 {

  DataSet personData = new DataSet(Measurer measureMore); 
  //error at Measurer "')' expected"

  data.add(new Person("Joe", 183));
  data.add(new Person("Chrissy", 158));
  data.add(new Person("Bobby", 175));

  double avg = data.getAverage();
  Person max = (Person) data.getMaximum();

  System.out.println("Average height: " + avg);
  System.out.println("Expected: 172.0");
  System.out.println("Name of tallest person: " + max.getName());
  System.out.println("Expected: Joe");
   }
}

As far as I can tell there should not be a missing parenthesis because the interface needs the variable, and I copy and pasted it from the class constructor itself which did compile properly.

Était-ce utile?

La solution 2

It looks like you're trying to declare or initialize a variable and pass in one step.

Try one of these options:

DataSet personData = new DataSet(new Measurer()); 

or if you need a reference to the Measure instance:

Measurer measureMore = new Measurer();
DataSet personData = new DataSet(measureMore); 

Autres conseils

When you pass a variable measureMore in a method call you don't put the type. You need to create the Measurer object first and then pass it to the constructor like:

DataSet personData = new DataSet(measureMore);

At least that's a start!

DataSet is an interface like you said, but you cannot instantiate an interface (can't use new). In order to use this, you would need to instead find or create a class that implements DataSet and then instantiate that.

The compiler goes on parsing as long as the code it has processed is the start of some valid program. If it gets to a point at which there is no valid continuation, it reports as "expected" one of the tokens that would have let it continue parsing.

In this case,

public class PersonTester
{
 public static void main(String[] args)
 {

  DataSet personData = new DataSet(

could have been valid. new DataSet would have had to be the start of an anonymous inner class declaration that implements DataSet, in which case the next token would have been ")", followed by the "{" opening the body of the class declaration.

As well as explaining the error message, this suggests an additional way of changing the code to be valid - use an anonymous inner class as the initializer.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top