File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Dushyant Maheshwary
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20369
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Dushyant Maheshwary
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47228
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

If you posted by mistake, I shall delete the duplicates.
 
Rob Spoor
Sheriff
Pie
Posts: 20369
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dushyant Maheshwary wrote:Finally, thank you for answering. That helped.

You're welcome
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can edit your post by using the button.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47228
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:You can edit your post . . .
Since he's new I'll do it for him, just this once
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic