Reputation: 451
here is a code i wrote in java, I'm still a beginner in a java so if this is a noob mistake please rectify it and also provide alternative solutions for reading the string and converting it to char array
import java.util.Scanner;
import java.io.*;
class TestClass {
public static void main(String args[] ) throws Exception {
Scanner s = new Scanner(System.in);
String str = s.nextLine();
char[] c = str.toCharArray(str);
int x=0,y=0;
for(int i=0;i<=str.length;i++)
{
if(c[i]=='L')
{
x=x+1;
}
else if(c[i]=='R')
{
x=x-1;
}
else if(c[i]=='U')
{
y=y-1;
}
else if(c[i]=='D')
{
y=y-1;
}
}
System.out.println(x+""+y);
}
}
I'm getting the following error
10: error: method
toCharArray
in class String cannot be applied to given types;no arguments String actual and formal argument lists differ in length 12: error: cannot find symbol
variable length variable str of type String
Upvotes: 0
Views: 2269
Reputation: 737
Combining answers (I don't take credit for these)
You put an argument in a function that doesn't take any:
char[] c = str.toCharArray(str);
Just use
char[] c = str.toCharArray();
-by Arc676
And also note that
it's str.length() not str.length
-by TomN
Both are correct answers
Also, I noticed that you have (I do take credit for these)
for(int i=0;i<=str.length;i++)
It should be '<', not '<=' to avoid an out of bounds exception
for(int i=0;i<str.length();i++)
Also, just a tip:
System.out.println(x+""+y);
I think you missed putting a space in those quotation marks.
Upvotes: 0
Reputation: 9625
Method str.toCharArray(str); doesn't exist. toCharArray takes no arguments. Replace with str.toCharArray()
Upvotes: 0
Reputation: 4465
You put an argument in a function that doesn't take any:
char[] c = str.toCharArray(str);
Just use
char[] c = str.toCharArray();
For more info, see the String documentation
Upvotes: 2