1,105,312 Community Members

IF statement not being executed

Member Avatar
deadsolo
Newbie Poster
20 posts since Oct 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi everyone!

I am having troubles making the second IF statment execute in my code:

Pattern dl_noise_rates_p = Pattern.compile("\\d+\\s(.+)\\s(.+)\\s(.+)\\s(.+)");
Matcher dl_noise_rates_m = dl_noise_rates_p.matcher(lineString2);
    if(dl_noise_rates_m.find()){
        String s_dl_nr1 = (dl_noise_rates_m.group(1));
        //checking for non numbers pulled in the regex
        if(s_dl_nr1 == "NaN"){ //| s_dl_nr1 == "NaN" | s_dl_nr1 == " NaN"
            System.out.println ("OMGOMGOMGOMGOMGOMGOMGOMGOMG ITS NOT A DECIMAL NUMBER");
            s_dl_nr1 = "0";
        }
        System.out.println ("Download noise rate value 1: " + s_dl_nr1);
    }

I am pretty sure the "OMGOMG" section should be getting outputted becasue when I execute my script I get this. Since I am seeing that the value of my string s_dl_nr1 is "NaN" I really don't understand why the 2nd IF statement is not executing.

Download noise rate value 1: NaN    

My regular expression is grabbing 4 numbers or NaN(s), storing them in variables (I didn't show all the code since its repetitive). I am trying to turn the NaN(s) string into "0" so I can convert it into a double like the rest of my values and then run some computations. Does anyone have any ideas what my issue could potentially be, or perhaps some type of work around? I really appreciate all help, ideas, comments, suggestions, etc.

Member Avatar
bguild
Posting Whiz
339 posts since Oct 2012
Reputation Points: 163 [?]
Q&As Helped to Solve: 84 [?]
Skill Endorsements: 8 [?]
 
0
 

When you use == between two object references it will only be true if those two references refer to the same object. Two String objects can easily contain the same chars without being the same object. For example, "NaN" == new String("NaN") will always be false even though both sides of the == contain 'N', 'a', 'N'. If you want to test two strings for containing the same chars, then you want to use the equals method.

Member Avatar
deadsolo
Newbie Poster
20 posts since Oct 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

That makes sense, however I am unsure of what the equals method is. I tried if(s_dl_nr1 = "NaN"), however that gives me the error "incompatible types". Can you elaborate a little more on the equals method please? Thank you for all your help again.

Member Avatar
stultuske
Posting Expert
5,290 posts since Jan 2007
Reputation Points: 935 [?]
Q&As Helped to Solve: 774 [?]
Skill Endorsements: 35 [?]
Featured
 
1
 

deadsolo: what bguild is trying to tell you is that you should only use == to compare the values of primitives.
if you are trying to compare the values of Objects, you should use the equals method. this is one of the very founding stones of the whole OO concept.
Object equals
String equals

Member Avatar
deadsolo
Newbie Poster
20 posts since Oct 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

AH thank you for the links stultuske! So I have switched to using the object equals and it has solved most of my issues. However I have instances where the string outputted says it is "NaN" or " NaN" but my if .equals statements are not catching it.

Here is the updated code:

if(s_dl_nr1.equals("NaN") | s_dl_nr1.equals("N/A") | s_dl_nr1.equals(" NaN")){ 
    System.out.println ("OMGOMGOMGOMGOMGOMGOMGOMGOMG ITS NOT A DECIMAL NUMBER Value 1");
    s_dl_nr1 = "0";
}

Here is the output:

OMGOMGOMGOMGOMGOMGOMGOMGOMG ITS NOT A DECIMAL NUMBER Value 3
OMGOMGOMGOMGOMGOMGOMGOMGOMG ITS NOT A DECIMAL NUMBER Value 4
Download noise rate value 1:  NaN   
Download noise rate value 2: NaN    
Download noise rate value 3: 0
Download noise rate value 4: 0

So some of the strings are being seen and some are not. Does anyone have any ideas what could be causing this issue? Thanks for all the help

Member Avatar
bguild
Posting Whiz
339 posts since Oct 2012
Reputation Points: 163 [?]
Q&As Helped to Solve: 84 [?]
Skill Endorsements: 8 [?]
 
0
 

In the output you posted there are 3 spaces following the value 1 NaN and 4 spaces following the value 2 NaN. The equals method will only return true if the two strings are exactly the same, so in this case:

"NaN    ".equals("NaN") // False!

I think you may find the String.trim method helpful.

Question Answered as of 1 Year Ago by bguild and stultuske
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: