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 3 of 3

Thread: Why don't if blocks work ?

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

    Default Why don't if blocks work ?

    I wrote a method but if blocks don't work, result is =0 male 0 female.
    How can ı fix it?
     public static void genderCount(String arr[][]){
            int maleCounter=0;
            int femaleCounter=0;
            for(int i =0;i<arr.length;i++){
                for(int j =0;j<arr[i].length;j++){
                    if(j==1){
                        if(arr[i][j]=="m" || arr[i][j]=="M" ){
                            maleCounter++;
                        }
                        else if(arr[i][j]=="F" || arr[i][j]=="f" ){
                            femaleCounter++;
                        }
                    }
     
                }
     
            }
            System.out.println(maleCounter+" male "+femaleCounter+" female");
        }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why don't if blocks work ?

    Use the equals method to compare the contents of two String objects, not the == operator.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why don't if blocks work ?

    Quote Originally Posted by Norm View Post
    Use the equals method to compare the contents of two String objects, not the == operator.
    it worked, thank you.

Similar Threads

  1. Blocks in Java
    By Me Geek in forum The Cafe
    Replies: 0
    Last Post: March 5th, 2021, 06:12 AM
  2. MouseListener doesn't work and It blocks KeyListener
    By JMoreno in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 25th, 2019, 09:32 AM
  3. [SOLVED] Multiple Catch Blocks
    By thecoderr in forum Java Theory & Questions
    Replies: 3
    Last Post: October 19th, 2017, 08:30 PM
  4. Representing tones visually in blocks
    By Nindustries in forum Java Theory & Questions
    Replies: 9
    Last Post: January 20th, 2013, 01:49 PM
  5. Nested try blocks
    By Ahmed. in forum Member Introductions
    Replies: 2
    Last Post: April 30th, 2010, 08:12 AM