如何在Java中的方法之间传递String Arrays?

时间:2022-04-26 21:32:07

I have an assignment that requires me to create a string array based on user input and then pass the array to another method. I also have to convert some of the string array to int (which I have no trouble doing). My problem is that whenever I'm trying to pass the array into the Public Lab method, I'm getting an error message saying that "The type of expression must be an array type but it resolved to string". Can someone please teach me how to fix this, I've been working on this for hours and it is due shortly.

我有一个赋值,要求我根据用户输入创建一个字符串数组,然后将该数组传递给另一个方法。我还必须将一些字符串数组转换为int(我没有遇到任何麻烦)。我的问题是,每当我尝试将数组传递给Public Lab方法时,我都会收到一条错误消息,指出“表达式的类型必须是数组类型,但它已解析为字符串”。有人可以教我如何解决这个问题,我已经在这方面工作了好几个小时,很快就会到期。

This is my code:

这是我的代码:

     import java.util.Scanner;
     public class Lab 
     {
    String[] input = new String[4];
    Scanner keyboard= new Scanner(System.in);{
    input[0] = keyboard.nextLine();
    String name = input[0];
    input[1] = "244";
    input[2] = "214";
    int classSize = Integer.parseInt(input[1]);
    int numTA = Integer.parseInt(input[2]);
    }

    public Lab(String input) // input is the string of form a,b,c 
    {
    String[] data = new String[4];
    data[0] = input[0]; //THIS IS AN ERROR!!!

        /* data[0] is lab name (ALL CAPS)
         * data[1] is lab enrollment
         * data[2] is # teaching assistants 
         * if less than 20 students per TA, set string to Very Well Covered
         * if at most 30 students per TA, set string to Well Covered
         * if at most 35 students per TA, set string to Covered
         * if more than 35 students per TA, set to Barely Covered
         * USE TYPE CONVERSIONS ^^^^^
         */

2 个解决方案

#1


3  

Make public Lab(String input) to public Lab(String[] input) to take String array as input parameter.

将公共Lab(String输入)设置为public Lab(String [] input)以将String数组作为输入参数。

#2


0  

You can even use variable arguments, like:

您甚至可以使用变量参数,例如:

public Lab(String... input){
//your code goes here
}

Variable arguments are used to pass any number of arguments as parameters. Though variable arguments are used when you give any number of arguments, use it carefully as it has certain limitations:

变量参数用于将任意数量的参数作为参数传递。虽然在提供任意数量的参数时会使用变量参数,但请谨慎使用,因为它有一定的局限性:

i. If you are going to have two or more arguments of same or different type, you must get the variable argument as the last parameter.

一世。如果要使用相同或不同类型的两个或多个参数,则必须将变量参数作为最后一个参数。

ii. You can not have more than one variable argument in your method or constructor.

II。您的方法或构造函数中不能有多个变量参数。

You can not use variable args as return type, instead which you can use arrays. You can have variable arguments for any primitive types or any objects(like int... a or float... b or Employee... emp). You can always use them just like arrays,i.e., you can access the elements by their index. I always use var args in my main method(public static void main(String... args){}).

您不能将变量args用作返回类型,而是可以使用数组。您可以为任何基元类型或任何对象(如int ... a或float ... b或Employee ... \ temp)提供变量参数。您可以像数组一样使用它们,即您可以通过索引访问元素。我总是在main方法中使用var args(public static void main(String ... args){})。

#1


3  

Make public Lab(String input) to public Lab(String[] input) to take String array as input parameter.

将公共Lab(String输入)设置为public Lab(String [] input)以将String数组作为输入参数。

#2


0  

You can even use variable arguments, like:

您甚至可以使用变量参数,例如:

public Lab(String... input){
//your code goes here
}

Variable arguments are used to pass any number of arguments as parameters. Though variable arguments are used when you give any number of arguments, use it carefully as it has certain limitations:

变量参数用于将任意数量的参数作为参数传递。虽然在提供任意数量的参数时会使用变量参数,但请谨慎使用,因为它有一定的局限性:

i. If you are going to have two or more arguments of same or different type, you must get the variable argument as the last parameter.

一世。如果要使用相同或不同类型的两个或多个参数,则必须将变量参数作为最后一个参数。

ii. You can not have more than one variable argument in your method or constructor.

II。您的方法或构造函数中不能有多个变量参数。

You can not use variable args as return type, instead which you can use arrays. You can have variable arguments for any primitive types or any objects(like int... a or float... b or Employee... emp). You can always use them just like arrays,i.e., you can access the elements by their index. I always use var args in my main method(public static void main(String... args){}).

您不能将变量args用作返回类型,而是可以使用数组。您可以为任何基元类型或任何对象(如int ... a或float ... b或Employee ... \ temp)提供变量参数。您可以像数组一样使用它们,即您可以通过索引访问元素。我总是在main方法中使用var args(public static void main(String ... args){})。