0

I have this sample from my project and I need to know why the result is what it is.

public class Main 
{   
    public static void main(String[] args)
    {
        //url: https://classicpartyrentals.com/products/24681-gothic-silver-coffee-cup, websiteList: http://classicpartyrentals.com/, URL Contains Returns bool: false
        String url = "https://classicpartyrentals.com/products/24681-gothic-silver-coffee-cup";
        String contains = "http://classicpartyrentals.com/";
        System.out.println("Returns bool: " + url.contains(contains));
    }

}

Output:

Returns bool: false
Eric Oudin
  • 259
  • 2
  • 4
  • 13

2 Answers2

2

Code is always doing what you ask it to do:

 String url = "https://classicpartyrentals.com/products/24681-gothic-

but

 String contains = "http://classicpartyrentals.com/";

https versus http!

So the real answer is: especially when you are a beginner, chances that your code uncovered "some Java bug" is relatively small (very close to zero in reality!)

There is a much higher chance that your assumptions are wrong. You either don't fully understand the methods you are calling, or there is a subtle flaw in your input data.

Finally: also work on your naming. contains isn't a very good name here; you better call it expectedUrl, or something alike!

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Oh i see thank you!! hard to catch things like that sometimes. – Eric Oudin Nov 04 '16 at 08:45
  • Sure. The way to go there is to have some other human person look at your code. But please understand that SO is not the ... well, perfect place ... for such inquiries. Better turn to your peers/coworkers and ask them to have a look! – GhostCat Nov 04 '16 at 08:47
  • Seems like this was the perfect solution for me. Not sure why this wouldn't be the place for help. I understand it would be quicker for me to ask another person but i asked people. They missed it too. I also do not always have access to someone to ask. After all this site is made to help with this sort of stuff anyway. – Eric Oudin Nov 04 '16 at 09:00
  • The real point is: its really just a subtle typo like bug. That is why you got those close requests. In that sense: feel free to accept the answer you like the most, and be happy that you didn't so too many downvotes so far ;-) – GhostCat Nov 04 '16 at 09:02
  • Yeah, I knew it would be something stupid but staring at it for hours was not helping. And contains is a method that String has already I didn't make it up. – Eric Oudin Nov 04 '16 at 09:05
  • 1
    I am not asking you change the name of an existing method. That one is fine. I am saying that you shouldnt call your **variable** "contains"! – GhostCat Nov 04 '16 at 09:06
  • Oh yeah. That makes sense. Thank you for the advice on everything BTW. – Eric Oudin Nov 04 '16 at 09:12
1

In your Code Url "https://classicpartyrentals.com/products/24681-gothic-silver-coffee-cup" contains https

but your compare string "http://classicpartyrentals.com/" contain http so its not match and return false

Hetal Rupareliya
  • 367
  • 3
  • 13