Java 字符串[]无法转换为字符串

Java 字符串[]无法转换为字符串,java,compiler-errors,static-methods,Java,Compiler Errors,Static Methods,所以我正在编写一个程序,要求用户输入数组的大小并用名称填充。稍后,我要求用户键入数字,程序将在数组中搜索具有长度的名称。但是,我不断得到一个不兼容的类型错误。字符串[]无法转换为字符串 package LAB4_1; import java.util.Arrays; import java.util.Scanner; public class LAB4_1 { public static void main(String[] args) { Scanner sc = new Sc

所以我正在编写一个程序,要求用户输入数组的大小并用名称填充。稍后,我要求用户键入数字,程序将在数组中搜索具有长度的名称。但是,我不断得到一个不兼容的类型错误。字符串[]无法转换为字符串

package LAB4_1;

import java.util.Arrays;
import java.util.Scanner;

public class LAB4_1

{

public static void main(String[] args)
{
    Scanner sc = new Scanner(System.in);
    String names [];
    int length_typed;
    String names_printed; 

    // Read the number of kids
    System.out.print("How many kids are there? ");
    int size = sc.nextInt();

    // Read the names of the kids
    String [] kids = new String [size];

    for (int k = 0; k < kids.length; k++)
    {
        System.out.print("Enter name of kid #" + k + ": ");
        kids[k] = sc.next();
    }
    // Print all the names of the kids and the length of their names
    System.out.println("Names of kids with lengths are: ");

    for (int i = 0; i < kids.length; i++)
    {
      System.out.println(kids[i] + " " + kids[i].length());
    }

    // Prompt for sequence of name lengths:
      System.out.print("Enter lengths of names as promted, 0 to terminate.");

    // Read the lengths and print list of names till 0 is entered
      length_typed = sc.nextInt();
字符串[]无法转换为字符串。我没有正确调用该方法吗

      System.out.println(names_printed);

}

/**
 * Count the number of names in an array that have a certain length
 * @param names: An array of names
 * @param length : an integer length
 * @return the number of names in the array whose length is given
 */

static int countByLength(String [] names, int length)
{
int count = 0;

for(int i = 0;i < names.length;i++)
{
  if(names[i].length() == length) 
      count++;
}


return count;
}


/**
 * Filter an array of names and keep only those names with a given length
 * @param names: an array of names
 * @param length: an integer length
 * @return the array of names that have the given length
 */
static String [] filterByLength(String [] names, int length)
{
    String [] filtered = new String[countByLength(names, length)];
    int index = 0;

    for(int k = 0; k < filtered.length; k++)
    {
        if(names[k].length() == length) filtered[index++]=names[k];
    }

    return filtered; 
}
System.out.println(打印的名称);
}
/**
*计算数组中具有特定长度的名称数
*@param names:名称数组
*@param length:一个整数长度
*@返回数组中给定长度的名称数
*/
静态int countByLength(字符串[]名称,int长度)
{
整数计数=0;
for(int i=0;i

}

否您没有正确调用该方法。您的方法
filterByLength
返回类型
字符串[]
。您正试图将该值放入
字符串
对象中,即
名称\u打印


那不行。将打印的
名称重新键入为
字符串[]
,您的代码应该可以工作。但是,您将无法使用
println
并获得可读的输出。您应该使用
println(Arrays.toString(names_printed))

尝试将字符串数组分配给字符串变量。我认为错误消息是完全清楚的,您将
String[]
filterByLength
方法返回给
String
变量
      System.out.println(names_printed);

}

/**
 * Count the number of names in an array that have a certain length
 * @param names: An array of names
 * @param length : an integer length
 * @return the number of names in the array whose length is given
 */

static int countByLength(String [] names, int length)
{
int count = 0;

for(int i = 0;i < names.length;i++)
{
  if(names[i].length() == length) 
      count++;
}


return count;
}


/**
 * Filter an array of names and keep only those names with a given length
 * @param names: an array of names
 * @param length: an integer length
 * @return the array of names that have the given length
 */
static String [] filterByLength(String [] names, int length)
{
    String [] filtered = new String[countByLength(names, length)];
    int index = 0;

    for(int k = 0; k < filtered.length; k++)
    {
        if(names[k].length() == length) filtered[index++]=names[k];
    }

    return filtered; 
}