【问题标题】:error:bad operand type for binary operation &&错误:二进制操作的操作数类型错误 &&
【发布时间】:2017-12-05 13:49:45
【问题描述】:

这是代码

import java.io.*;
class WORD2
 {  
public void main() throws IOException
{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the sentence");
        String str=br.readLine().toUpperCase()+" ";
        int l=str.length();
        String word="";
        int p=0;
        String word2[]=new String[l];
        for(int i=0;i<str.length();i++)
        {
            char ch=str.charAt(i);
            **if((str.charAt(i))&&(str.charAt(i+1)!= ' '))**// THE ERROR COMES IN THIS STATEMNET SHOWING THE ERROR
            {
                if(ch!=' ')
                {
                    word=word+ch;   //TAKING EACH WORD
                }
                else 
                {
                    word2[p++]=word;  //STORING EACH WORD
                    word="";
                }

        }}
        String q="";
            for(int k=0;k<p;k++)
            {
                q=q+word2[k]+" ";
            }
            System.out.print("q="+q);
        }}

它是一个去除句子中单词之间不必要的空格的程序。

【问题讨论】:

    标签: bluej


    【解决方案1】:
    if((str.charAt(i)!= ' ' )&&(str.charAt(i+1)!= ' '))
    

    是正确的语法,因为 IF 需要考虑 bool (true/false)。 正如你所拥有的,第一部分返回字符。 另外,检查 str.charAt(i+1) 否则你会得到 StringIndexOutOfBoundsException!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-24
      • 2013-04-22
      相关资源
      最近更新 更多