-1

@DavidMarciel the code is now supposed to store the received value in the corresponding variable after checking that some condition is true, but when the program doesn't enter the body of if-condition even though the condition is true. I put an example in this code for the "male" variable being true, and placed a print statement inside the body of the if to show it. Below is my code.

public class j {
static String sss = "male,O+,45,saudi,brain_diseases";
static String male = "";
static String blood = "";
static String age = "";
static String nat = "";
static String dis = "";
static void func() {
    String[] pieces = sss.split(",");
    male = pieces[0];
    blood = pieces[1];
    age = pieces[2];
    nat = pieces[3];
    dis = pieces[4];

    System.out.println(male);
    System.out.println(blood);
    System.out.println(age);
    System.out.println(nat);
    System.out.println(dis);
    //
if(male=="male"){
System.out.println("hello male");
}
}

public static void main(String[] args) {

    func();        
}}
Ciba
  • 47
  • 8
  • `@user` in a question body doesn't do anything. – Savior Apr 21 '16 at 16:17
  • This appears to be an attempt at replying to an answer on another question, namely "[Storing array of strings in specifies variables respectively](http://stackoverflow.com/questions/36747075/storing-array-of-strings-in-specifies-variables-respectively)". – dcsohl Apr 21 '16 at 16:26

1 Answers1

1

Replace

if(male=="male"){

with

if (male.equals("male")) {

The operator == test to check if two objects are the same objects. Instead the method .equals check for the content of the strings in this case.

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
  • 1
    The appropriate action here is to vote to close as a duplicate of the well-known canonical [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Savior Apr 21 '16 at 16:21
  • Surely the wrong answer is to downvote a right answer – Davide Lorenzo MARINO Apr 21 '16 at 16:23