0

I'm a bit of a novice to Java, and I've written some code that doesn't give the output I'm expecting.

if (!"".equals(S_USD_EXCH_RATE)
        && !S_USD_EXCH_RATE.equalsIgnoreCase("---")) {
    exchRate = Double.parseDouble(S_USD_EXCH_RATE);
}
if (S_NW < 0 || (S_FA / S_NW) > 4)
    return -1;
else if ((S_NW * exchRate) > 75000000.0 && (S_FA / S_NW) < 1.5)
    return 3;
else if ((S_NW * exchRate) > 25000000.0 && (S_FA / S_NW) < 2)
    return 2;
else if (S_NW == 0)
    return 0;
else if ((S_NW * exchRate) >= 1 && (S_FA / S_NW) < 4)
    return 1;
else
    return 1;

When S_NW is blank, a value of 1 is returned, when it's supposed to be 0. I can't figure out why as when S_NW == 0 (which should cover the situation of no value as well, as I understand), an output of0 should be returned. Any ideas?

As per the comments: I was under the impression java knew 0 to be either the number value of 0 or no value. Is that not correct? I have a program provided by an external body written in java where you can create new objects, you get the option of creating a number object, date, string etc. The S_NW object is a number. The program allows the object to have no data assigned to it, hence the term blank, or no value. Hope that helps.

As per further comment:

I don't know what 'Number' refers to exactly, the program is closed source. On your information you've provided, I can add another line to tell it what to do in the case of NULL and see if that's any better. From the comments, it should fix it.

Further to comments:

Yes that's all I have to work with. The suggested S_NW != null check gives me an exception error, incomparable types: double and I guess that makes S_NW a double data type

Further information:

I have tried wrapping the code in if S_NW = Double.NaN {}; else return 0 and I have replaced else if (S_NW == 0) with else if (S_NW == 0 || S_NW == 0.0 || S_NW == Double.NaN) and I still get the output of 1 on both occasions. This doesn't make any sense as those two edits contradict each other, suggesting S_NW is both == Double.NaN and not equal to Double.NaN which can't be right. Unless I'm being really thick?

Simkill
  • 357
  • 1
  • 3
  • 17
  • Please format your code following at least [Oracle Formatting Guidelines](http://www.oracle.com/technetwork/java/codeconventions-150003.pdf) and provide us with an [MCVE](https://stackoverflow.com/help/mcve) – wonderb0lt Jul 17 '15 at 08:31
  • define "blank". What type of variables are they, and how and where do you declare them? if you do it on class level, they'll have default values. – Stultuske Jul 17 '15 at 08:33
  • So... what data type does `S_NW` have? A string or a number? How can something be both blank and zero? – maja Jul 17 '15 at 08:33
  • And please add the information asked in above comments by editing your question, not by adding comments. It's easier to read and understand the question that way. – TDG Jul 17 '15 at 08:34
  • I think I spotted some magic numbers (see http://stackoverflow.com/questions/47882/what-is-a-magic-number-and-why-is-it-bad) – maja Jul 17 '15 at 08:34
  • @maja I doubt the op to be looking for magic numbers at this point, rather getting a basic knowledge foundation of programming. if it's an int, and declared locally, with no assignment of value, it could be 'blank', but then it would cause a compile error. The question seems to vague to come to a complete (correct) answer – Stultuske Jul 17 '15 at 08:36
  • @Stultuske You're right, magic numbers shouldn't be his/her concern right now. – maja Jul 17 '15 at 08:38
  • As for your edit: Java knows `0` (zero) and `null` (no value), which have two completly different meanings. Depending on your datatype, the variable can have the value `null` (String, Integer, ...), or it can't (int) - which type do you use? ("Number" can refer to `Integer`, `Long`, `int`, `float`, ...). To check if `S_NW ` has a value assigned, you can use `if(S_NW != null){}` – maja Jul 17 '15 at 08:40
  • Is this really all the code you have? You have to work with variables and don't know which type they have? This seems to be odd. What is the value of `exchRate` if the first `if` is not true? – maja Jul 17 '15 at 08:53
  • @Simkill What is the datatype for of `S_NW` ? If it is an object as you said, please post the codes for the class of `S_NW`. – user3437460 Jul 17 '15 at 08:54
  • Since `S_NW` is double, it's possible it has a non zero value that is almost 0, so that when printed it shows 0. That would result in 1 being returned, as that's what's returned by the code when all the checks fail (the last check being useless). Using `==` to compare doubles is [almost always wrong](http://stackoverflow.com/questions/8081827/how-to-compare-two-double-values-in-java). – kiheru Jul 19 '15 at 09:56
  • @kiheru that is good thinking. I have tried <= 1 instead of == 0 but that still returns 1 as well. – Simkill Jul 22 '15 at 16:30
  • Just noticed another possible problem: the comparison against NaN is incorrect. Nan is not equal to any number, *including itself*. You should use `Double.isNan()` instead. It's hard to say what you mean by "blank", but if NaN is allowed, that is the correct way to check it. – kiheru Jul 22 '15 at 18:31

2 Answers2

0

Seeing as if S_NW is 0 (S_FA / S_NW) will throw an exception, we can conclude that S_NW isn't zero. Depending on data type and how you initialize it you might either have a bug where it gets set anyway or you have initialized it with a value different from 0. I would guess it's an Integer or something where null isn't handled as a number at all. So you need a null check as well.

Astrogat
  • 1,617
  • 12
  • 24
  • That makes sense seeing as you can't divide by 0. Maybe the blank value is being treated as 1. That's something I can look at. – Simkill Jul 17 '15 at 09:30
  • I think it's more probable that it's treated as null. If it's an Integer/Double and not a primitive type. – Astrogat Jul 17 '15 at 09:33
  • In my question above I have tried checking to see if it's a null value, but I'm struggling. I understand you can't just check for a straight `null` as double's don't support `null`, and it's `Double.NaN` instead, (or something like that?) I've tried checking (as per question update) but it doesn't seem to matter what I do, the output is still 1. – Simkill Jul 17 '15 at 09:38
  • There is a difference between double and Double. double is a primitive type, and it can not be null. Double can be null. Double d = null will give you an object that is equal to null. double d = null will give you d == 0. – Astrogat Jul 17 '15 at 09:39
  • Judging by my error messages, it's a `double` and not a `Double`. how is a double treated when no value is assigned, or does it depend on the program? – Simkill Jul 17 '15 at 09:42
0

If S_NW is an int, it cannot be null.
If S_NW is a String originally, you can try this to test if it is null when you parse it to int

       String S_NW="";
       int s_NW_INT;
       try{
            S_NW_INT=Integer.parseInt(S_NW);
        }catch(NumberFormatException ex){
            System.out.println("S_NW is not an int");
        }
K.Hui
  • 153
  • 1
  • 13