This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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: 19655
    
  18

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: 38045
    
  22
Welcome to the Ranch

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

Joined: Oct 27, 2005
Posts: 19655
    
  18

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: 38045
    
  22
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.
 
Similar Threads
It should be true.........
problem with method "validRoomNumber
Confusing TRUE and FALSE
Why is it printing true?
StackOverflowError using hashCode() in HashMap in recursive method