8 Replies - 2142 Views - Last Post: 12 March 2011 - 10:29 AM Rate Topic: -----

#1 paki123   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 88
  • Joined: 18-February 11

Why isn't my if statement working?

Posted 12 March 2011 - 12:53 AM

For those who don't know exactly what group.result is, it is what you get when you use match result.

The reason I'm asking is because I thought group.result(1(or whatever number)) should return a string.

And for some reason, even when I hardcode my if statement, it still doesn't work.

for example System.out.println(group.result(1)) prints out 5


then shouldn't "5" = group.result(1)?


because for some reason when using an if statement and i put that in there, it doesn't work.

like
if(group.result(1) == "5"){
do this
}

it doesn't do this :(.

Is This A Good Question/Topic? 0
  • +

Replies To: Why isn't my if statement working?

#2 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4241
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: Why isn't my if statement working?

Posted 12 March 2011 - 01:22 AM

Hey.

I seem to recall that using == to compare strings doesn't actually compare the strings, but rather checks if the strings are the same object. (That is: it compares object references, not the value of the object.)

Try group.result(1).equals("5") instead.
Was This Post Helpful? 1
  • +
  • -

#3 Igneus   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 17
  • Joined: 27-December 09

Re: Why isn't my if statement working?

Posted 12 March 2011 - 01:25 AM

Well, according to the link below, "group()" in the matchresult interface does return a string. I'm not sure I understand your code of "group.result()" as it doesn't seem to be a method in the matchresult interface. Now as far as your if statement goes, have you tried casting your call to "group.result(1)" as a string? Or have you tried setting the if statement to 5 instead of "5"?

Edit: Atll, I believe you are correct. Completely forgot about that, I do believe Java compares string references with the == command.

This post has been edited by Igneus: 12 March 2011 - 01:28 AM

Was This Post Helpful? 1
  • +
  • -

#4 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4241
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: Why isn't my if statement working?

Posted 12 March 2011 - 02:02 AM

Good point about the result() method, Igneus. I only went as far as to check the API docs for the MatchResult.group method. - There doesn't seem to be any result() method in anything related to it.

Could you perhaps give us a link to that method, paki123, or the code if it is your own?
Was This Post Helpful? 0
  • +
  • -

#5 paki123   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 88
  • Joined: 18-February 11

Re: Why isn't my if statement working?

Posted 12 March 2011 - 09:29 AM

You were correct about the .equals method. I didn't know the == compared the string references.

Here is how I got to result.group(1)*

not group.result(1)

Scanner s = new Scanner(line);                   
                          
        String pattern = (.*);
        s.findInLine(pattern);
        
        MatchResult result = s.match();
        System.out.println(result.group(1));




It does return a string. lols sorry i mistyped the result.group(1), it was a long and stressful night. THANKS!!!

Also, if i wanted to do "not equal to", would I do something like this:

result.grou(1) = "5" for this example

if(!(result.group(1).equals("3)"))) { [...] }


Is that how you do an not equal to?

This post has been edited by paki123: 12 March 2011 - 09:30 AM

Was This Post Helpful? 0
  • +
  • -

#6 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4241
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: Why isn't my if statement working?

Posted 12 March 2011 - 09:57 AM

Yea, that would work. The ! flips the boolean bit, so it basically reads as "NOT".
You could also write that as:
if (result.group(1).equals("3)") == false)



I'm guessing the extra ) next to the 3 is a typo though? :)
Was This Post Helpful? 0
  • +
  • -

#7 paki123   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 88
  • Joined: 18-February 11

Re: Why isn't my if statement working?

Posted 12 March 2011 - 10:18 AM

Yea you are right. Hah. Also, is there a way to use regex in my comparison? I wanted to check if(result.group(1).equals("(\\d*)") but it didn't work.

Is there a way to do it?
Was This Post Helpful? 0
  • +
  • -

#8 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4241
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: Why isn't my if statement working?

Posted 12 March 2011 - 10:26 AM

String.equals is just for String to String comparison, but you should be able to use String.matches to match against a regular expression.
Was This Post Helpful? 1
  • +
  • -

#9 paki123   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 88
  • Joined: 18-February 11

Re: Why isn't my if statement working?

Posted 12 March 2011 - 10:29 AM

Wow, I can't believe how helpful everyone here is. I really appreciate it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1