Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 赋值的左侧必须是变量字符_Java_String_Variable Assignment - Fatal编程技术网

Java 赋值的左侧必须是变量字符

Java 赋值的左侧必须是变量字符,java,string,variable-assignment,Java,String,Variable Assignment,我目前有以下java代码 public class lesson8 { static Console c; // The output console public static void main (String[] args) { c = new Console (); String user; int length, counter, spacecounter; spacecou

我目前有以下java代码

public class lesson8
{
    static Console c;           // The output console

    public static void main (String[] args)
    {
        c = new Console ();

        String user;
        int length, counter, spacecounter;
        spacecounter=0;
        c.print("Enter a string. ");
        user = c.readLine();

        length = (user.length()-1);

        for (counter=0;counter<length;counter++) 
        {
            if (user.charAt(counter) = "") 
            {
                spacecounter++;
            }
        }

        c.println("There are "+spacecounter+" spaces in your string.");
        c.println("There are "+counter+" characters in your string.");

        // Place your program here.  'c' is the output console
        // main method
    }
}
错误是

赋值的左侧必须是变量

我将其更改为“==”,但现在我得到另一个错误:

左侧子表达式“char”的类型与右侧子表达式“java.lang.String”的类型不兼容

我该如何解决这个问题


谢谢

您要使用相等运算符
=
,而不是赋值运算符
=

您要使用相等运算符
=
,而不是赋值运算符
=

“==”将确保右侧的值与左侧的变量相同

“=”是赋值运算符,用于为变量赋值,而不是比较变量

“==”将确保右边的值与左边的变量相同


“=”是赋值运算符,用于为变量赋值,而不是比较变量

在比较运算符上使用asignment

改变

if (user.charAt(counter) = "") 

更新:
您在比较时再次出现错误。 您还应该使用
单引号(')
来比较
char
,否则它将无法编译

if (user.charAt(counter) == '')  
但这也不会被编译,因为没有定义零长度字符。

您应该比较一个有效字符,比如说“”代表空格。

您在比较运算符上使用的是asignment

改变

if (user.charAt(counter) = "") 

更新:
您在比较时再次出现错误。 您还应该使用
单引号(')
来比较
char
,否则它将无法编译

if (user.charAt(counter) == '')  
但这也不会被编译,因为没有定义零长度字符。
您应该比较一个有效字符,比如说“”代表空格。

那么,原因是什么

if (user.charAt(counter) = "") 
给出的错误是“=”是java中的赋值运算符,因此左侧必须是变量。话虽如此,你可能真的想要

if (user.charAt(counter) == ' ')
它使用比较运算符(=)和空格字符(“”)。(“”是一个空字符串)

那么,原因是什么

if (user.charAt(counter) = "") 
给出的错误是“=”是java中的赋值运算符,因此左侧必须是变量。话虽如此,你可能真的想要

if (user.charAt(counter) == ' ')

它使用比较运算符(=)和空格字符(“”)。(“”是一个空字符串)

我的代码中出现了相同的错误。添加括号解决了此错误

if(isNotNullorEmpty(operator)) 
                ArrayList<String> result =  getOperatorAndTypeforName(name );
if(isNotNullorEmpty(运算符))
ArrayList结果=GetOperator和TypeForName(名称);

if(isNotNullorEmpty(运算符)){
ArrayList结果=GetOperator和TypeForName(名称);
}

我的代码中也有同样的错误。添加括号解决了此错误

if(isNotNullorEmpty(operator)) 
                ArrayList<String> result =  getOperatorAndTypeforName(name );
if(isNotNullorEmpty(运算符))
ArrayList结果=GetOperator和TypeForName(名称);

if(isNotNullorEmpty(运算符)){
ArrayList结果=GetOperator和TypeForName(名称);
}

我将其更改为“==”,但现在又出现另一个错误。。。“char”中左边子表达式的类型与右边子表达式“java.lang.String”的类型不匹配。我该如何解决这个问题?将您的比较与字符ie
'
,而不是字符串
'
。我将其更改为“==”,但现在我得到另一个错误。。。“char”中左边子表达式的类型与右边子表达式“java.lang.String”的类型不匹配。我该怎么解决这个问题呢?用一个字符(即
'
)来比较,而不是一个字符串
'
。实际上。。。那些“”应该是“”。否则+1。不能有空字符文本。它可能应该是“”,并带有空格。@templatetypedef是的,在回答中提到实际上。。。那些“”应该是“”。否则+1。不能有空字符文本。它可能应该是“”,带有空格。@templatetypedef Yes,答案中提到过