Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: CodingBat code efficency question

  1. #1
    Junior Member
    Join Date
    Sep 2018
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default CodingBat code efficency question

    https://codingbat.com/prob/p191914

    Doing this CodingBat problem, and I just was wondering about the solution I came up with compared to theirs. Obviously neither is "wrong" but I was just wondering (in the long run) which is the better syntax / more efficient code?

    Their Solution
        public String notString(String str) {
      if (str.length() >= 3 && str.substring(0, 3).equals("not")) {
        return str;
      }
     
      return "not " + str;
    }
     
    And my solution
     
    public String notString(String str) {
      if(str.indexOf("not") == 0){
        return str;
      }
      return "not " + str;
    }

    And my solution

    public String notString(String str) {
      if(str.indexOf("not") == 0){
        return str;
      }
      return "not " + str;
    }

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: CodingBat code efficency question

    Either way is fine although I like yours better. You could have also used the startsWith(String str) method of String. I would suggest you don't worry about efficiency. All things equal, most attempts at making code more efficient tend to fail or is an effort in diminishing returns. I would focus on correctness, documenting your code, and making it easy to read. The person after you who needs to maintain it will be grateful.

    Also, notice that this does not detect a string which begins just with the word "not" as the word "nothing" as well as others will pass the test.

    Regards,
    Jim

Similar Threads

  1. Question about my code
    By justforeva in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 27th, 2017, 04:02 PM
  2. I need a java code for this question. can any one help me..?
    By Vinod Kumar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 11th, 2014, 09:50 AM
  3. Code for a question???
    By bickey in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 19th, 2013, 05:36 PM
  4. Java code for a Question
    By Pushpinder in forum Loops & Control Statements
    Replies: 1
    Last Post: September 24th, 2012, 07:13 AM
  5. This is my code and i would like to ask a question..
    By savvas in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 24th, 2011, 01:01 PM