Java constructor cannot find symbol error ' = new'

1k views Asked by At

I am trying to initialize one Java class from within a conditional in another class - I want MarsRovers to initialize Rover. I am getting a 'cannot find symbol' error when I try to initialize a Rover object from MarsRovers. I am new to Java so I have a feeling it has something to do with the scope of plateauCoords and inputLines. I've tried other solutions I've seen on here, but they aren't working for my problem (like making my variables public).

The goal is to eventually make a new Rover as long as inputLines % 2 is equal to 0 (with an until loop).

Here is the MarsRover code:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class MarsRover {
  public static void main(String []args) throws FileNotFoundException {
    Scanner console = new Scanner(System.in);
    System.out.println("Mars rover is ready for input, please enter name of input file: ");
    String filename = console.nextLine();
    console.close();
    List<String> inputLines = new ArrayList<String>();

    Scanner scanner = new Scanner(new File(filename));
    scanner.useDelimiter("\n");
    while(scanner.hasNext()){
      inputLines.add(scanner.next());
    }
    String plateauCoords = inputLines.get(0);
    inputLines.remove(0);
    scanner.close();
    System.out.println(inputLines);

    if(inputLines.size() % 2 == 0) {
      MarsRover rover = new Rover(plateauCoords, inputLines);
    } else {
      System.out.println("Your directions are not formatted correctly");
    }
  }
}

And here is the Rover code:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class Rover {
  public Rover(String platCoords, String[] input) {
    System.out.println("INSIDE ROVER");
  }
}

When I compile MarsRovers.java, I get this error:

MarsRover.java:27: cannot find symbol
symbol  : constructor Rover(java.lang.String,java.util.List<java.lang.String>)
location: class Rover
      MarsRover rover = new Rover(plateauCoords, inputLines);
                        ^
1 error
2

There are 2 answers

1
khelwood On BEST ANSWER

Your Rover constructor as defined takes an array of strings. You are trying to call the constructor with a list of strings. A list of strings is a different thing from an array of strings. You could fix it by, for instance, converting the list to a string array.

Rover rover = new Rover(plateauCoords, inputLines.toArray(new String[inputLines.size()]));

Note additionally that you can't assign an object of type Rover to a variable of type MarsRover, because (as defined) they are entirely different types.

1
Jack On

The type of List<String> can't be assigned to String[] so typechecking fails.

The former is a List (actually the runtime type is narrower since List is just an interface) generic instance of String while the latter is an array of String objects.

You should convert your List<String> to an array. JDK already provides this functionality:

String[] array = inputLines.toArray(new String[inputLines.size()]);