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

Thread: Need Help with Writing methods that involves Loops

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need Help with Writing methods that involves Loops

    Hello Forum, I am a new user and I have been at this problem for over 2 hours with my friends. We honestly don't know what's wrong and if could help us that would be awesome

    The question is,

    Given a string, return the longest substring that appears at both the beginning and end of the string without overlapping. For example, sameEnds("abXYab") is "ab".

    Example are:
    sameEnds("abXYab") -> "ab"
    sameEnds("xx") -> "x"
    sameEnd("x") -> "x"

    What we have tried so far is:

    public static String sameEnds(String str)
        {
            int len = str.length();
            String output = "";
            boolean b = true;
            if(len == 0 || len == 1)
                return str;
            if(len == 2 && str.charAt(0) == str.charAt(1))
                return str.charAt(0) +"";
            if(len == 3 && str.charAt(0) == str.charAt(2))
                return str.charAt(0) +"";
            for(int i = 0; i < len / 2; i++)
            {
                if(str.substring(i,i+1).equals(str.substring(len-(i+1),(len-i))) && b)
                    output += str.substring(i,i+1);
                else
                    b = false;
            }
            return output;
        }

    If anyone could help us it would be great.

    Also, another problem I am having is with this question:

    The Evil E is back and it brought back some friends! Write a method that will acknowledge that the letter E is evil. Each letter E will eliminate that characters around it. But the Evil E's will not eliminate its friends. So it will no eliminate the letters e or E.

    Example are:

    evilEs("HELLO") -> " E LO"
    evilEs("dEep") -> " Eep"
    evilEs("Evil") -> "E il"
    evilEs("bean") -> "bean"
    evilEs("AbEEmgmEeodE") -> "A EE g Eeo E"
    evilEs("ELEPHANT") -> "E E HANT"
    evilEs("HomepagE") -> "Homepa E"

    I haven't really tried anything for this one but any help is good help.

    Thank you for reading this


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Need Help with Writing methods that involves Loops

    I wrote a quick program using your method, and here are the results:

    racecar --> rac
    races race -->
    corn -->
    sisters --> s
    bordiogbo -->

    So, only sisters and corn worked correctly. Racecar should have returned r, races race should have returned race, and bordiogbo should have returned bo.

    So, your program picks up palidromic patterns (backwards = forwards), but I don't think that's what you want. Your program starts at the ends of the String and works inward, but you want the letter patterns to occur in the same order (as I understand it). I think the best way to do this is to start with your substring comparisons at half the String's length, then work down instead of starting your for-loop at 0. My reasoning for this is so: if you start with a small substring, your program may notice that the entire String both starts and ends with "s". However, it might then overlook the fact that the String also starts and ends with "sas". By starting with the largest possible common substring, you eliminate the possibility of overlooking matching substrings.

    Also, note that your substrings will not just be one character long. As in my example of "races race," you can see that the common ending is actually four characters long. However, you program only assumes common endings of length one because it uses this code:
    str.substring(len-(i+1),(len-i))
    str.substring(i,i+1)

    This is difficult to get my ideas across, but hopefully this helps some.

    Now, to your next problem...

    Think about what your program needs to do: it needs to check each letter of a String to see if it is an "E," and then remove characters from the String in indexes that are one less and one greater than the indexes of found E's (unless! the characters in those positions are E's or e's). I'd start simple: write a program that can check each character of a String.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help with Writing methods that involves Loops

    I finished the first problem and ended up getting:

    public static String sameEnds(String str)
        {
            int len = str.length();
            String output = "";
            if(len==0)
                return "";
            if(len==1)
                return str;
            if(len == 2 && str.charAt(0)== str.charAt(1))
                return str.charAt(0) +"";
            if(len == 3 && str.charAt(0)== str.charAt(2))
                return str.charAt(0) +"";
            for(int i = 0; i < len / 2; i++)
            {
                if(str.substring(0,len/2-i).equals(str.substring(len/2+i+1,len)) &&len %2 ==1)
                    return str.substring(0,len/2-i);
                if(str.substring(0,len/2-i).equals(str.substring(len/2+i,len)))
                    return str.substring(0,len/2-i);
            }
            return output;
        }

    as for the second one, i have this so far:

    public static String evilEs(String str)
        {
            String output = "";
            if(str.indexOf('E') == -1)
                return str;
            else
            {
               for(int i = 0; i < str.length(); i++)
               {
                   String temp = "X" + str + "X";
                   if(temp.charAt(i) == 'E' && (temp.charAt(i - 1) != 'E' || temp.charAt(i - 1) != 'e') && (temp.charAt(i + 1) != 'E' || temp.charAt(i + 1) != 'e'))
     
               } 
            }
            return output;
        }
    and the problem im having is subtracting the index before and after i from the string.

  4. #4
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Need Help with Writing methods that involves Loops

    I finished the first problem
    Does it work?

    String temp = "X" + str + "X";
    Why are you doing this?

    the problem im having is subtracting the index before and after i from the string.
    Okay. Think about what you are actually doing here: instead of thinking of this as removing the characters, think about it as taking every character BUT the ones next to E's. So, you need to take substrings from before and in between the characters you want to remove.

    To visualize what I'm saying, think of this example:

    evilE's("crEate")

    crEate

    The black sections are the parts of the String you want to keep, and you can do this by adding substrings together.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  5. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help with Writing methods that involves Loops

    I am using the temp to avoid the out-of-bounds error for when E is the first letter of a String.
    Yes the first problem works.

  6. #6
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Need Help with Writing methods that involves Loops

    I am using the temp to avoid the out-of-bounds error for when E is the first letter of a String.
    Okay. There is a small problem with this: if there is an E as the first character of the String, the X will get removed, and then when you try to get rid of the character where you THINK the X is, it's actually the E, and then you have loads of problems... Instead, I'd just use if statements to account for the ends of the String (ie, if(i == str.length() - 1) or if(i == 0), then have the program react accordingly).

    if(temp.charAt(i) == 'E' && (temp.charAt(i - 1) != 'E' || temp.charAt(i - 1) != 'e') && (temp.charAt(i + 1) != 'E' || temp.charAt(i + 1) != 'e'))

    I'd also revise this; what if the character before an E is g and after is e? This statement overlooks that possibility by requiring BOTH of the characters surrounding the E to not be E or e. Instead, I would first check if the current character is E, and then put separate if statements within the first if statement to check the surrounding characters one at a time.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  7. #7
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help with Writing methods that involves Loops

    Hi,

    The aproximmation is like palindrome words. If you search palindrome program, you can find a lot.

    Oscar Gomez
    Engineer at PSL
    PSL S.A. - CMMi 5 nearshore software development and outsourcing from Latin America - Home

  8. #8
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: Need Help with Writing methods that involves Loops

    and the problem im having is subtracting the index before and after i from the string.

    I agree with the group, to read 3 characters from your string at a time, then store them in past, current, and next

    it is true that you would like to know what past is, but this is handled as you iterate through your string.

    special cases: for the first character you will have no past, and for the last character you will have no next.

    you are working with current. so for your first character you write a special case for it before you jump into your loop.

    when you are running your loop start by only caring about what current wants. check it for being an E then do accordingly,
    move all your variables over, and store past every time. since past has been checked for Es on both sides it is safe to add!
    then update next by collecting it from your string. repeat the loop until you meet the end condition and write code to handle the second special case.

    goodluck,
    Jonathan.

Similar Threads

  1. Writing to DTD Document
    By DanG1775 in forum Java SE APIs
    Replies: 4
    Last Post: December 12th, 2011, 04:41 PM
  2. Having trouble figuring out this method. Involves while-loops!
    By ayelleeeecks in forum Loops & Control Statements
    Replies: 4
    Last Post: October 19th, 2011, 08:10 PM
  3. Ok, I have an assignment that involves lists, it's in C+, and also polynomials
    By javapenguin in forum Other Programming Languages
    Replies: 18
    Last Post: October 9th, 2011, 11:40 AM
  4. [SOLVED] Writing a program with arrays and class methods... PLEASE HELP?
    By syang in forum What's Wrong With My Code?
    Replies: 17
    Last Post: August 8th, 2011, 07:02 AM
  5. Writing to files
    By nitwit3 in forum Java Theory & Questions
    Replies: 3
    Last Post: July 25th, 2011, 04:00 AM