简体   繁体   English

Java构造函数找不到符号错误'= new'

[英]Java constructor cannot find symbol error ' = new'

I am trying to initialize one Java class from within a conditional in another class - I want MarsRovers to initialize Rover. 我正在尝试从另一类的条件内初始化一个Java类-我希望MarsRovers初始化Rover。 I am getting a 'cannot find symbol' error when I try to initialize a Rover object from MarsRovers. 尝试从MarsRovers初始化流动站对象时,出现“找不到符号”错误。 I am new to Java so I have a feeling it has something to do with the scope of plateauCoords and inputLines. 我是Java的新手,所以我觉得它与PlateauCoords和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). 目标是只要inputLines%2等于0(使用直到循环),最终制作一个新的流动站。

Here is the MarsRover code: 这是MarsRover代码:

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: 编译MarsRovers.java时,出现以下错误:

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

The type of List<String> can't be assigned to String[] so typechecking fails. 无法将List<String>的类型分配给String[]因此类型检查失败。

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. 前者是一个List (实际上是运行时类型为较窄,因为List只是一个interface )的通用实例String ,而后者是一个数组String对象。

You should convert your List<String> to an array. 您应该将List<String>转换为数组。 JDK already provides this functionality: JDK已经提供了此功能:

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

Your Rover constructor as defined takes an array of strings. 您所定义的Rover构造函数采用字符串数组。 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. 另外请注意,您不能将Rover类型的对象分配给MarsRover类型的变量,因为(定义为)它们是完全不同的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM