[英]reading a string from scanner and converting it to character array
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 这是我在Java中编写的代码,我仍然是Java的初学者,因此,如果这是一个菜鸟错误,请更正它,并提供替代方法来读取字符串并将其转换为char数组
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;10:错误:无法将String类中的
toCharArray
方法应用于给定类型;no arguments String actual and formal argument lists differ in length 12: error: cannot find symbol
无参数字符串实际和形式参数列表的长度不同12:错误:找不到符号
variable length variable str of type String
可变长度String类型的可变str
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 有关更多信息,请参见String文档。
Method str.toCharArray(str); 方法str.toCharArray(str); doesn't exist.
不存在。 toCharArray takes no arguments.
toCharArray不接受任何参数。 Replace with str.toCharArray()
替换为str.toCharArray()
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 由Arc676
And also note that 还要注意
it's str.length() not str.length 它是str.length()而不是str.length
-by TomN 由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. 我认为您错过了在这些引号中添加空格的情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.