Java charAt 错误“char 无法转换为字符串”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29270624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:42:55  来源:igfitidea点击:

charAt error "char cannot be converted to string"

javacompiler-errors

提问by Hugo Pakula

I am trying to use the following code so that given a string, give the length of the longest contiguous subsequence of the same character. I am getting the error "incompatible types: char cannot be converted to java.lang.String". I have commented where the error is being found below.

我正在尝试使用以下代码,以便给定一个字符串,给出相同字符的最长连续子序列的长度。我收到错误“类型不兼容:char 无法转换为 java.lang.String”。我已经评论了下面发现错误的位置。

public class Test {
    public int longestRep(String str)
    {
        int currLen = 1;
        String currLet = "";
        String maxLet = "";
        int maxCount = 0;
        int currPos = 0;
        int strLen = str.length();
        for(currPos = 0; currPos < strLen; currPos++)
        {
            currLet = str.charAt(currPos); //error is on this line
            if(currLet = str.charAt(currPos+1))
            {
                currLen++;
            }
            else
            {
                if(currLen > maxLen)
                {
                    maxLen = currLen;
                    maxLet = currLet;
                    currLen = 1;
                }
            }
        }
    }
    public static void main(String args[])
    {
        longestRep("AaaaMmm");
    }
}

采纳答案by MadProgrammer

  • currLet = str.charAt(currPos);A Stringvalue can't be assigned to a char, they are different types, apples and oranges
  • if (currLet = str.charAt(currPos + 1)) {is actually an assignment (make currLetequal to the value of str.charAt(currPos + 1))
  • if (currLen > maxLen) {- maxLenis undefined
  • You never returnanything from the method...
  • currLet = str.charAt(currPos);String值不能被分配给一个char,它们是不同的类型,苹果和桔子
  • if (currLet = str.charAt(currPos + 1)) {实际上是一个赋值(使currLet等于的值str.charAt(currPos + 1)
  • if (currLen > maxLen) {-maxLen未定义
  • 你永远不会return从方法中得到任何东西......

Try changing:

尝试改变:

  • String currLet = "";to something more like char currLet = '\0';and String maxLet = "";to char maxLet = '\0';
  • if (currLet = str.charAt(currPos + 1)) {to something like if (currLet == str.charAt(currPos + 1)) {
  • Add int maxLen = 0to your variable declerations (may be under int maxCount = 0)
  • String currLet = "";到更多的东西一样char currLet = '\0';,并String maxLet = "";char maxLet = '\0';
  • if (currLet = str.charAt(currPos + 1)) {if (currLet == str.charAt(currPos + 1)) {
  • 添加int maxLen = 0到您的变量声明中(可能在 下int maxCount = 0

Now, based on your example code, public int longestRep(String str) {will need to be public static int longestRep(String str) {in order for you to call from you mainmethod...

现在,根据您的示例代码,您public int longestRep(String str) {需要public static int longestRep(String str) {从您的main方法中调用...

回答by Paul

String.charAt(int)returns a character. But currLetis of type String, so you can't assign a character. Use currLet = Character.toString(str.charAt(currPos));instead.

String.charAt(int)返回一个字符。但是currLet是 type String,所以你不能分配一个字符。使用currLet = Character.toString(str.charAt(currPos));来代替。

回答by ajb

As the compiler said, you can't convert a charto a String. If you have a charand you really want to convert it to a Stringof length 1, this will work:

正如编译器所说,您不能将 a 转换char为 a String。如果您有 achar并且您确实想将其转换String为长度为 1 的 a,则可以这样做:

String s = String.valueOf(c);

or

或者

String s = Character.toString(c);

However, if the character you're working with was obtained by charAt, another solution is to get rid of charAtand use substringto return a string of length 1:

但是,如果您正在使用的字符是由 获得的charAt,另一种解决方案是摆脱charAt并使用substring返回长度为 1 的字符串:

currLet = str.substring(currPos, currPos + 1);

回答by Dias

Easy way to convert Char to String

将字符转换为字符串的简单方法

Add an empty String to the start of expression, because adding char and String results into String.

将一个空字符串添加到表达式的开头,因为将 char 和 String 添加到 String 中。

Convert one char"" + 'a'
Convert multiple chars"" + 'a' + 'b'

转换一个字符"" + 'a'
转换多个字符"" + 'a' + 'b'

Converting multiple chars works because "" + 'a'is evaluated first.
If the ""were at the end instead you would get "195"

转换多个字符有效,因为"" + 'a'首先评估。
如果""是在最后,你会得到"195"

Remember that Java language guarantees (Java Language Specification, Java SE 7 Edition, section 15.12.4.2) that all arguments are evaluated from left to right (unlike some other languages, where the order of evaluation is undefined)

请记住,Java 语言保证(Java 语言规范,Java SE 7 版,第 15.12.4.2 节)从左到右评估所有参数(与某些其他语言不同,评估顺序未定义)