my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Can't figure where I went wrong. Help needed. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can Watch "Can New topic
Author

Can't figure where I went wrong. Help needed.

Dushyant Maheshwary
Greenhorn

Joined: Jun 07, 2010
Posts: 7
Hi all,
I'm new to java and have been practicing coding on CodingBat (which I feel is an excellent resource for a beginner).

There is a string based program which says: Given a string, return true if the string starts with "hi" and false otherwise.

I wrote the following code for it:



But during runtime, it doesn't return true for the following inputs:
startHi("hi there")
startHi("hi ho")

Where am I going wrong?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

1) UseCodeTags
2) Don't use == for String comparison - that checks if the two String references are referencing the exact same object. Since substring creates a new String object that's not the case here. Use the .equals method instead:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Dushyant Maheshwary
Greenhorn

Joined: Jun 07, 2010
Posts: 7
First of all, I'm really sorry for posting the same thing 3 times. But it wasn't intentional. The first 2 times when I clicked on the submit button, I got a message that JavaRanch is maintaining its servers, so please try in a while. I never checked to see if the message was posted.
Secondly, I'm new here and will slowly learn the rules of the forum. I'll use Code Tags from the next time.
Finally, thank you for answering. That helped.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
Welcome to the Ranch

If you posted by mistake, I shall delete the duplicates.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

Dushyant Maheshwary wrote:Finally, thank you for answering. That helped.

You're welcome
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You can edit your post by using the button.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
David Newton wrote:You can edit your post . . .
Since he's new I'll do it for him, just this once
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can't figure where I went wrong. Help needed.