Java String[] 无法转换为 String

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28403963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:10:09  来源:igfitidea点击:

String[] cannot be converted to String

javacompiler-errorsstatic-methods

提问by Kapaz

So I'm writing a program where i ask the user to enter size of array and fill it with names. Later i ask user to type number and the program will search array for names that have length. However i keep getting a incompatible type error. String[] cannot be converted to string.

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

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();

The error is here

错误在这里

      names_printed = LAB4_1.filterByLength(names, length_typed);

String[] cannot be converted to String. Am i not calling the method correctly?

String[] 不能转换为 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; 
}

}

}

采纳答案by k_g

No you are not calling the method correctly. Your method filterByLengthreturns the type String[]. You are trying to place the value in a Stringobject, namely names_printed.

不,您没有正确调用该方法。您的方法filterByLength返回类型String[]。您正在尝试将该值放在一个String对象中,即names_printed.

That doesn't work. Retype your names_printedas a String[]and your code should work. However, you won't be able to use printlnand get a readable output. You should use println(Arrays.toString(names_printed)).

那行不通。重新键入您names_printed作为 aString[]和您的代码应该工作。但是,您将无法使用println并获得可读的输出。你应该使用println(Arrays.toString(names_printed)).