Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Error with binary operand.

  1. #1
    Junior Member
    Join Date
    Dec 2021
    Posts
    26
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Error with binary operand.

    The error for the following code is:
    StringCharacter.java:13: error: bad operand types for binary operator '!='
             if(st.charAt(i) != " ")
                             ^
      first type:  char
      second type: String
    1 error

    // program to demonstrate String class
    public class StringCharacter{
     
       public static void main(String args[]){
     
          String st = "This is my first program.";
          int count = 0;
          int length = st.length(); // returns length of string 
     
          // increment count for each character excluding spaces
          for(int i = 0; i < length; i++){
             if(st.charAt(i) != " ")
             count++;
     
          }
          System.out.println("The number of characters in this string is: " + count);
       }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Error with binary operand.

    A char constant is defined by enclosing the character with single quotes: '
    A String constant is defined by enclosing the character(s) with double quotes: "

    Also look at some of the static methods in the Character class that will test if a char is a space and return a boolean value.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    FightingIrishman (January 11th, 2022)

Similar Threads

  1. Bad Operand for Binary Type Error (Can't figure this out)
    By fuarian in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 27th, 2019, 02:25 PM
  2. Replies: 7
    Last Post: January 23rd, 2013, 09:04 PM
  3. [SOLVED] Bad operand types for binary operator '<' error issue (beginner)
    By mju516 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 27th, 2012, 10:33 PM
  4. Binary datafile and object creation according to binary tag ?
    By loicus in forum Object Oriented Programming
    Replies: 4
    Last Post: October 14th, 2011, 01:00 PM
  5. Solution for error message: Bad operand for binary operator '-'
    By MostinCredible21 in forum AWT / Java Swing
    Replies: 8
    Last Post: September 7th, 2011, 04:28 PM