简体   繁体   English

不能通过方法调用转换将String []转换为int吗?

[英]String[] cannot be converted to int by method invocation conversion?

What this code is trying to do 这段代码试图做什么

Desired result: 所需结果:

Decipher the meaning of the following quote, using code: 使用代码解密以下引用的含义:

Also, you can use the modulus operator to extract the rightmost digit or digits from a number. 另外,您可以使用模运算符从数字中提取最右边的一个或多个数字。 For example, x % 10 yields the rightmost digit of x (in base 10). 例如,x%10产生x的最右边数字(以10为底)。 Similarly x % 100 yields the last two digits. 同样,x%100会产生最后两位数字。 - Think Java, Allan Downey, Green Tea Press -想想Java,艾伦·唐尼,绿茶出版社

Questions 问题

  • How is this error repaired? 如何修复此错误?
  • Can my code be simplified or consolidated (see comments)? 我的代码可以简化还是合并(请参见注释)?

Error message 错误信息

extRight.java:11: error: method extRight in class 
extRight cannot be applied to given types;
                int s = extRight(args);
                        ^
  required: int
  found: String[]
  reason: actual argument String[] cannot be 
  converted to int by method invocation conversion
1 error

My code 我的密码

class extRight {

    public static void extRight(int x) {

        x = x % 10; //initially tried `x % 10;`

    }

    public static void main(String[] args){

            //initially the below was: extRight(args);
            //then: System.out.print(extRight(args));
        int s = extRight(args);
        String o = Integer.toString(s);
        System.out.print(o);

    }

}

Existing function 现有功能

First of all, change the name of your function, extRight , to something else! 首先,将函数的名称extRight更改为其他名称! It's the name of the function's class,which would be used for defining constructors of that class. 它是函数类的名称,该类将用于定义该类的构造函数。

How to assign a new value to an argument 如何为参数分配新值

x is an argument, and you cannot modify arguments like x = x % 10; x是一个参数,您不能修改x = x % 10;类的参数x = x % 10; . Either declare a static variable, int x , and directly access it; 要么声明一个静态变量int x ,然后直接访问它; or, return int from your function! 或者,从您的函数返回int! Otherwise, your function will not be useful. 否则,您的功能将无用。 Ex.: 例:

//return x
public static int extRight1(int x) {

    x = x % 10;
    return x;

}

Passing wrong type 传递错误的类型

You are passing args to extRight , which expects an int-type parameter: int s = extRight(args); 您正在将args传递给extRight ,它需要一个int类型的参数: int s = extRight(args); .

Automatic string conversation 自动字符串对话

You don't have to take this step: String o = Integer.toString(s); 您不必执行此步骤: String o = Integer.toString(s); . You can directly write System.out.print(s); 您可以直接编写System.out.print(s); , as it will automatically convert the int in sysout to String before printing it to console. ,因为它将在打印到控制台之前将sysout中的int自动转换为String。

Working code 工作代码

Working code would look something like what follows (with my understanding of what you want to achieve): 工作代码如下所示(根据我对您要实现的目标的理解):

class extRight {

public static int extRight1(int x) {

    x = x % 10; 
     return x;

}

public static void main(String[] args){
    //do not forget  to pass command line args to program like this "java extRight 1"   
    int s = extRight1(Integer.parseInt(args[0]));// change indices to assign different arg[] values
   //  String o = Integer.toString(s)  you dont have to do this you can directly write
    System.out.print(s);

}

}
extRight(args) // extRight() expects an int, you are passing a String[]

check 校验

public static void extRight(int x) { // expecting int. Also int s = extRight(args); --> this method returns nothing i.e, void

    x = x % 10; //initially tried `x % 10;`

}

At first glance there are two things wrong here (possibly quite a few more). 乍一看,这里有两件事是错误的(可能还有很多)。 Look at your definition for the extRight method: 查看您对extRight方法的定义:

public static void extRight(int x)

Specifically the types. 具体类型。 The argument is an int and the return is void . 参数intreturn值为void Now see how you use it: 现在看看如何使用它:

int s = extRight(args);

args is a String[] (hence the compiler error), and what do you expect s to become if there's no return type? argsString[] (因此会发生编译器错误),如果没有返回类型,您期望s变成什么?

It's not clear at all what this code is even trying to do, but to "correct the error" you would need to align your types correctly. 甚至还不清楚该代码试图做什么,但是要“更正错误”,您需要正确对齐类型。 Simply changing the argument to a String[] wouldn't really work, because the method's logic needs an int . 仅将参数更改为String[]并不会真正起作用,因为该方法的逻辑需要一个int Maybe you need to convert the first args value to an int and pass that to the method? 也许您需要将第一个args值转换为int并将其传递给方法? Then you'll also want to return something from that method it seems... 然后,您还想从该方法中返回一些东西,似乎...

As you can see or have wirten 如你所见

String[] args

args is an array of String -Objects argsString -Objects的数组

Your method 你的方法

void extRight(int x)

however takes an int as argument. 但是以int作为参数。 The primitive type int and the Object type String are not compatible. 基本类型int和对象类型String不兼容。 Plus an array of objects is not compatible with a single object of the same type. 另外,对象数组与相同类型的单个对象不兼容。 You need to Iterate over the array of your arguments and do an explicit conversion/parsing of the String value to an int value. 您需要遍历参数数组,并将String值显式转换/解析为int值。

The statement 该声明

x = x % 10;

has not the effect that you expect, in java parameters are passed by value (references too), so when you modify x inside the method, the modified value is visible only in that method. 不会产生您期望的效果,因为在Java参数中按值(也就是引用)传递参数,所以当您在方法内部修改x时,修改后的值仅在该方法中可见。 (It's not like in C where you can pass a pointer to an int!) (这与在C中可以将指针传递给int的方式不同!)

finally your method extRight has the the return type void which will give you error on the line 最后您的方法extRight的返回类型为void ,这将使您在行上出错

int s = extRight(args);

Your method needs to be 您的方法需要

int extRight(int x)

Here is a modified version of your class 这是您课程的修改版本

class ExtRight {
    public static int extRight(int x) {
        return x % 10;
    }

    public static void main(String[] args){
        for(String s : args) {
            int i = extRight(Integer.parseInt(s));
            System.out.print(i);
        }
    }
}

Also note that you should not name a method with the same name of The class, and that classnames should always start with an Upper case letter. 另请注意,您不应使用与该类相同的名称来命名方法,并且该类名应始终以大写字母开头。

args are string-type. args是字符串类型的。 You need to convert arg to Int type. 您需要将arg转换为Int类型。

Here is modified code: 这是修改后的代码:

class extRight {

public static int extRight(int x) {

    x = x % 10; //initially tried `x % 10;`
    return x;

}

public static void main(String[] args) {

    int i = Integer.parseInt(args[0]) //convert to int
    int s = extRight(i);
    String o = Integer.toString(s);
    System.out.print(o);
}
}

You have to parse your 'args' String tab into an integer : 您必须将“ args”字符串标签解析为整数:

int t = 0;
try {
    t = Integer.parseInt(args[0]);
} catch (Exception e) {
    // error : the first argument is not an integer
    t = 0;
}
s = extRight(t);

There are some very basic problems with your code. 您的代码有一些非常基本的问题。

The first problem is that you have a String[] and try to treat it as an int . 第一个问题是您拥有String[]并尝试将其视为int A String[] may contain multiple values (even 0 values!), so it's not clear which one you want to treat as a number. 一个String[]可能包含多个值(甚至0个值!),因此不清楚要将哪个数字视为数字。 I'll just assume you want the very first one. 我假设您要第一个。

The second problem is the problem of return types and call-by-value in your extRight function. 第二个问题是extRight函数中的返回类型和按值调用的问题。 Assigning a value to x inside the function will overwrite only the local variable x . 在函数内部为x赋值将仅覆盖局部变量x It doesn't change the variable of the calling function (your main ). 它不会更改调用函数(您的main )的变量。

Here's my guess on what you want to achieve and my proposed solution: 这是我对您要实现的目标以及我建议的解决方案的猜测:

class extRight {
    //this function now returns a value so that the changes by your calculations
    // can be used by the main function
    public static int extRight(int x) {
        return x % 10;
    }

    public static void main(String[] args){
        //this converts the first argument to an int:
        int arg = Integer.valueOf(args[0]);
        //the return value of your extRight function is store to the variable s
        int s = extRight(arg);
        //instead of your code, you can simply print the int variable
        System.out.print(s);
    }
}

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

相关问题 “无法通过方法调用转换将其转换为String []” - “cannot be converted to String[] by method invocation conversion” 无法通过方法调用转换将JSONObject转换为String - JSONObject cannot be converted to String by method invocation conversion 实际参数Date无法通过方法调用转换转换为int - actual argument Date cannot be converted to int by method invocation conversion 实际参数long不能通过方法调用转换转换为int - actual argument long cannot be converted to int by method invocation conversion 实际参数int不能通过方法调用转换转换为byte - Actual argument int cannot be converted to byte by method invocation conversion 不能通过方法调用转换将实际参数String转换为int-如何修复它 - Actual argument String cannot be converted to int by method invocation conversion - how to fix it 在方法调用转换时,实际列表不能转换为字符串 - Actual List cannot be converted to String on method invocation conversion 无法通过方法调用转换将问题转换为char [] [] - Issue with cannot be converted to char[][] by method invocation conversion 实际参数…无法通过方法调用转换转换为… - actual argument … cannot be converted to … by method invocation conversion 实际参数不能通过方法调用转换进行转换 - actual argument cannot be converted by method invocation conversion
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM