• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

If you posted by mistake, I shall delete the duplicates.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


You're welcome
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can edit your post by using the button.
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic