If statement with multiple input conditions

I have written some simple code, but I have an issue with it. I am not a Java developer, but am willing to learn!

I have two input fields with one output field. When these two fields are populated it works as expected, however in certain scenarios , only the first field will appear - no second field or value. The code does not work on the single field only.

public String CheckValues(String a,String b,Container container){

if (a.equals("Z4") && b.equals("X"))

return"W";

else{if (a.equals("Z4"))

return"H";}

return"";

}

Is there a way I can make this work?

Can someone point me in the right direction?

[1169 byte] By [BazStoresa] at [2007-11-27 9:13:43]
# 1

Try this:

public String CheckValues(String a,String b,Container container){

if ("Z4".equals(a) && "X".equals(b))

return "W";

else { if ("Z4".equals(a))

return "H";}

return "";

}

In fact:

null.equals("abc")

generates an error wheras

"abc".equals(null)

works (return false)

Hope That Helps

java_2006a at 2007-7-12 22:01:20 > top of Java-index,Java Essentials,Java Programming...
# 2

try this

public String CheckValues(String a,String b,Container container){

String str = "";

if (a.equals("Z4") && b.equals("X")){

str = "W";

}else if(a.equals("Z4")){

str = "H";

}

return str;

}

Yannixa at 2007-7-12 22:01:20 > top of Java-index,Java Essentials,Java Programming...
# 3

Thanks for your helpful response.

I have tried applying that code, but the problem persists.

The problem I have is that when both fields exsist, the mapping function works. If one field does not exsist, the mapping does not work. It is not an empty field - there is no field, so String B does not exist and the new field is not created.

BazStoresa at 2007-7-12 22:01:20 > top of Java-index,Java Essentials,Java Programming...
# 4
While mapping you have to map exact function or else it will not work..
jenny_07a at 2007-7-12 22:01:20 > top of Java-index,Java Essentials,Java Programming...
# 5

> It is not an

> empty field - there is no field, so String B does not

> exist and the new field is not created.

What do you mean by B does not exist... do you want to say that B is null.. right? In that case what is the output you are getting? Are you getting any exception/error?

diptaPBa at 2007-7-12 22:01:20 > top of Java-index,Java Essentials,Java Programming...
# 6

I don't what you mean wiht "B does not exists", anywaym this should work

public String CheckValues(String a,String b,Container container){

String str = "";

if (a.equals("Z4") && b != null && !b.trim().equals("") && b.equals("X")){

str = "W";

}else if(a.equals("Z4")){

str = "H";

}

return str;

}

pbulgarellia at 2007-7-12 22:01:20 > top of Java-index,Java Essentials,Java Programming...
# 7

Try overloarding the method. You will just need to know which strings you have when you make the call to CheckValues. For example, CheckValue(oneStirng, twoString, acontainer) will call the first method, but

CheckValue(oneString, acontainer) will call the second.

public String CheckValues(String a, String b, container container){

if(a.equals("Z4") && b.equals("X"))

return "W";

return "";

}

public String CheckValues(String a, Container container){

if(a.equals("Z4")

return "H";

return "";

}

Jstatsa at 2007-7-12 22:01:20 > top of Java-index,Java Essentials,Java Programming...