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 4 of 4

Thread: Lab 11.2; trying to shorten code

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool Lab 11.2; trying to shorten code

    Here are the instructions:

     
    Write a static method endsWith that inputs two Strings and returns a boolean. If the first input ends with the substring that is the second input, then the method returns true; otherwise, it returns false.

    Here is my code:

     
    public static boolean a (String inputOne, String inputTwo)
     
    {
     
    if (inputOne.contains(inputTwo)){
     
        if (inputOne.contains(inputOne.substring(inputOne.lastIndexOf(inputTwo[0]),
    inputOne.length())))
     
        {
     
        return true;
     
        }
     
    }
     
    return false;
     
    }

    I'm wondering if I could do this in an easier way with something similar to the "replace" function is the replacing characters in a substring, in order to simplify it. I know my code so far doesn't work.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Lab 11.2; trying to shorten code

    The ol' fashioned way would be to use a reverse loop and compare chars. As soon as a mismatch is found return false.

    Simplifying your attempt replace inner if statement with: return (index of second String in first String) equals (length of first String minus length of second String)
    Improving the world one idiot at a time!

  3. #3
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Lab 11.2; trying to shorten code

    My code got a little bit longer. Here it is now:

     
    public static boolean a (String inputOne, String inputTwo){
    if (inputOne.contains(inputTwo)){
        if inputOne.length() > inputTwo.length(){
    return ((inputOne.length() - inputTwo.length()).equals(inputOne.index(inputTwo)));
        }
    }
     
    // subtract length of inputTwo from inputOne to find index for substring 
     
    return false;
    }

    What is a reverse loop?

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Lab 11.2; trying to shorten code

    Keep in mind that shorter code doesn't always mean faster running code. Your goal should be to utilize speed by finding the shortest path from point A to point B. Short code isn't good code if it runs slowly.

    The code you provided probably doesn't compile. But, you can do a tradeoff of extra memory for less computations. When to do this depends on what the method is doing, but given you are just messing around with ints, you could have something like this:
    int index = inputOne.indexOf(inputTwo);
    if (index >= 0) { // IndexOf returns -1 if the String is not found
    	int diff = inputOne.length()-inputTwo.length();
    	if(diff>0) { // If inputOne.length() is > inputTwo.length, subtracting the two would be a positive number
    		return diff==index; // We have already calculated the difference and found the index, so we are saving a few computations
    	}
        }
    }
    return false;
    It's more costly to memory in the short-term, but given the values you are saving are very small in size, the extra use of memory should out-match the extra computations we didn't have to do.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. is something wrong with my code? Lab 10.1
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 17
    Last Post: October 28th, 2013, 09:05 PM
  2. Need Help with While Loop Lab
    By LennDawg in forum Other Programming Languages
    Replies: 4
    Last Post: June 29th, 2012, 10:41 AM
  3. Replies: 2
    Last Post: February 7th, 2012, 04:26 PM
  4. Help with a lab!
    By Spicy McHaggis in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 2nd, 2012, 08:03 AM
  5. PLEASE HELP ME WITH MY COLLEGE LAB!
    By crsoccerplayer6 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 29th, 2011, 04:06 PM

Tags for this Thread