'String[] cannot be converted to int by method invocation conversion?
What this code is trying to do
Desired result:
Solove the following word problem, 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). Similarly x % 100 yields the last two digits.
- Think Java, Allan Downey, Green Tea Press
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);
}
}
Solution 1:[1]
Existing function
First of all, change the name of your function, extRight
, to something else! 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;
. Either declare a static variable, int x
, and directly access it; or, return int from your function! 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);
.
Automatic string conversation
You don't have to take this step: String o = Integer.toString(s);
. You can directly write System.out.print(s);
, as it will automatically convert the int in sysout to String before printing it to console.
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);
}
}
Solution 2:[2]
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;`
}
Solution 3:[3]
At first glance there are two things wrong here (possibly quite a few more). Look at your definition for the extRight
method:
public static void extRight(int x)
Specifically the types. The argument is an int
and the return
is 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?
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
. Maybe you need to convert the first args
value to an int
and pass that to the method? Then you'll also want to return something from that method it seems...
Solution 4:[4]
You have to parse your 'args' String tab into an integer :
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);
Solution 5:[5]
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
. A String[]
may contain multiple values (even 0 values!), so it's not clear which one you want to treat as a number. 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. Assigning a value to x
inside the function will overwrite only the local variable x
. It doesn't change the variable of the calling function (your 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);
}
}
Solution 6:[6]
As you can see or have wirten
String[] args
args
is an array of String
-Objects
Your method
void extRight(int x)
however takes an int
as argument. The primitive type int
and the Object type String
are not compatible. 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.
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. (It's not like in C where you can pass a pointer to an int!)
finally your method extRight
has the the return type void
which will give you error on the line
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.
Solution 7:[7]
args are string-type. You need to convert arg to Int type.
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);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | TheLostMind |
Solution 3 | David |
Solution 4 | Frédéric Filée |
Solution 5 | f1sh |
Solution 6 | |
Solution 7 | Wolfpack'08 |