如何修复我的大学目录的服务器类中的此"error: array required, but String found"错误?



这是代码的摘录。

 while(true) { 
     System.out.print("Enter New Command: ");
     Scanner scan = new Scanner(System.in);
     String myLine = scan.nextLine(); //finds out whether command is to add,find, or delete
     String[] splitInfo = myLine.split(" ");
     if (myLine[0].equals("find")) { //*****ERROR********
        d.find(myLine[1]); //*****ERROR HERE******
     }

我记下了我的错误发生在哪一行。我做错了什么,我应该如何修复它?

myLine是你的字符串,splitInfo你的数组。你必须改变

if (myLine[0].equals("find")) {
    d.find(myLine[1]);
 }

if (splitInfo[0].equals("find")) {
    d.find(splitInfo[1]);
 }

问题if (myLine[0].equals("find")) .

这应该是if (splitInfo[0].equals("find")).

请注意,d.find(myLine[1]);也应更改为 d.find(splitInfo[1]);

这将修复它

while(true) { 
    System.out.print("Enter New Command: ");
    Scanner scan = new Scanner(System.in);
    String myLine = scan.nextLine();
    String[] splitInfo = myLine.split(" "); //The array which has tokens of myline
    if (splitInfo[0].equals("find")) { //look for 'find' in splitInfo
    d.find(splitInfo[1]);
}

最新更新