Results 1 to 6 of 6
  1. #1
    Calaminh is offline Member
    Join Date
    Nov 2011
    Posts
    15
    Rep Power
    0

    Question I don't know what I'm doing wrong in my substring.

    Java Code:
        /**Determines if the passed in String is made of two words
         * in a row.  For example: "HelloHello"
         * Precondition str's length is even
         * @param str The String to be considered
         * @return The String that comes first using the alphabet.
         */
        public static boolean isADoubleWord(String str)
        {
            int len = str.length();
            int halfLength = (len/2);
            
            if(str.substring(0 , halfLength) == str.substring(halfLength , len))
            {
                return true;
            }
           
            else
            {         
                return false;     
            }

    RUNNER:

    Java Code:
    public class StringExtrasRunner
    {
    public static void main(String[] args)
    {
    String[] words = {"hello","World","Sacco", "roxrox"} ;
    System.out.println("Testing firstASCII and lastASCII...");
    for(int i = 0; i<words.length ;i++)
    for(int j = i+1; j<words.length ;j++)
    {
    System.out.println(StringExtras.firstASCII(words[i],words[j])+" comes before "+StringExtras.lastASCII(words[i],words[j]));
    }
    
    System.out.println("\nTesting firstAlpha and lastAlpha...");
    for(int i = 0; i<words.length ;i++)
    for(int j = i+1; j<words.length ;j++)
    {
    System.out.println(StringExtras.firstAlpha(words[i],words[j])+" comes before "+StringExtras.lastAlpha(words[i],words[j]));
    }
    
    System.out.println("\nTesting isCapitalized...");
    for(int i = 0; i<words.length ;i++)
    {
    System.out.println( words[i]+" "+(StringExtras.isCapitalized(words[i])? "is":"is not")+ " capitalized.");
    }
    
    System.out.println("\nTesting isADoubleWord...");
    
    for(int i = 0; i<words.length ;i++)
    {
    System.out.println( words[i]+" "+(StringExtras.isADoubleWord(words[i])? "is":"is not")+ " a double word.");
    }
    
    }
    }
    RESULT:

    Testing isADoubleWord...
    hello is not a double word.
    World is not a double word.
    Sacco is not a double word.
    roxrox is not a double word.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: I don't know what I'm doing wrong in my substring.

    don't test String equality with ==. The equality operator, ==, tests if two String variables refer to the same String object, and this is not what's important to you, and in fact is misleading. Instead you want to know if the two Strings contain the same characters in the same order, and for that use the String method equals(...) or equalsIgnoreCase(...).

  3. #3
    Calaminh is offline Member
    Join Date
    Nov 2011
    Posts
    15
    Rep Power
    0

    Default Re: I don't know what I'm doing wrong in my substring.

    Thank you very much Fulbarable! Added to your reputation.

    I need help with another set of objects. After I've initialized the variables in the constructor I'm clueless on how to do the rest, if you can help me with the first three objects I'm confident that with the help I can solve the rest (no friends are on at the moment, and I did try to Google [looked at your how to ask a question in your signature])

    Java Code:
    public class Time
    
    {
    
        private int hrs, min,sec;
    
        private boolean pm;
    
       
    
        private final int SEC_IN_MIN = 60;
    
        private final int MIN_IN_HRS = 60;
    
        private final int SEC_IN_HRS = 3600;
    
       
    
       
    
        //Constructs a Time object and sets the hours, minutes, and seconds to h,m,s.  The incoming time is 24 hour time and is converted to 12 hour time
    
        public Time(int h, int m, int s)
    
        {
            hrs = h;
            min = m;
            sec = s;
        }
    
    
       
    
        //increments the time one second, checking to see if the minutes and hours increases.  Also checks if am/pm needs to be changed.
    
        public void increment()
    
        {
     
            
            
        }
    
       
    
        //increments the time a specific number of hours, minutes and seconds checking to see if the minutes and hours increases as a result of rollover.  Also checks if am/pm needs to be changed.
    
        public void increment( int h, int m, int s)
    
        {
    
           
    
        }
    
       
    
        //returns true if it is pm, false if am
    
        public boolean isPM()
    
        {
    
           
    
        }
    Edit: I didn't want to make a new thread even though it's a new problem, I don't want to spam the forum.
    Last edited by Calaminh; 11-08-2011 at 05:05 AM.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: I don't know what I'm doing wrong in my substring.

    Quote Originally Posted by Calaminh View Post
    I'm clueless on how to do the rest
    In future please ask a specific question and you will get a specific answer. "I don't know what to do" is not specific and can lead to a gazillion responses none of which help you. For example: Have a beer.

    If you are refering to implementing the empty methods then stepping away from the computer and working things out on paper is a good first step. Then you can take things one step at a time. For the increment() method the comment mentions increasing the time by one second. Surely this is a simple task. However once that has been done you have to check if you need to tick over the minute (9:15:59 becomes 9:16:00 not 9:15:60). Then check if you have to tick over the hour. Then check to see if you need to tick over am/pm. None of which is hard, you just need to use some if statements and changing the value of some variables. All of which you should have done in the past. So grab your paper and start thinking.

  5. #5
    Calaminh is offline Member
    Join Date
    Nov 2011
    Posts
    15
    Rep Power
    0

    Default Re: I don't know what I'm doing wrong in my substring.

    Thank you Junky for that advice. You are correct in every way, I can't expect anyone to do my HW and spoon feed me the answers.

    I was able to brainstorm various if that would eventually be used, but I'm having trouble in the formatting of it. I know that there should be these if statements:

    (brackets are excluded in these examples)

    if(hrs > 12)
    return 1;

    if(min > 60)
    return hrs+1;

    somehow make the remainder of any extra value be added on to it:

    int A = min - 60;

    return min + A;



    if(sec > 60)
    return minutes+1;

    somehow make the remainder of any extra value be added on to it:

    int B = sec - 60;

    return sec + B;


    The question I should be asking is: how do I format the if statements to include the increment of the extra values?

    Thank you.
    Last edited by Calaminh; 11-08-2011 at 05:46 AM.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: I don't know what I'm doing wrong in my substring.

    What are you returning? The increment() method is void so it should return nothing. You are kinda on the right track but you have to get things in the correct order. My brief explanation in the previous post should have guided you. You have to increase seconds no matter what so that is the first thing to do.
    Java Code:
    increment seconds
    if seconds is 60 {
        set seconds back to 0
        increment minutes
    }
    // keep going for minutes and hours

Similar Threads

  1. Extarctin substring
    By cowboy2010 in forum New To Java
    Replies: 15
    Last Post: 08-02-2010, 04:49 AM
  2. substring Q
    By arsenal4ever_11 in forum NetBeans
    Replies: 1
    Last Post: 06-01-2010, 03:27 PM
  3. Help with substring
    By hiklior in forum New To Java
    Replies: 12
    Last Post: 06-28-2008, 06:48 AM
  4. How can i get substring
    By 82rathi.angara in forum New To Java
    Replies: 14
    Last Post: 06-27-2008, 04:35 AM
  5. substring
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-11-2007, 08:15 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •