Results 1 to 7 of 7
  1. #1
    jimmy-lin is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default Beginner, need a little help

    Hi, I'm new here and a beginner in java.

    Can some one give me an example how to code a string that can performs:

    1. make a string, and return 3 copies of the last two characters

    For example: "Hi" will be "HiHiHi" or "Hello" will be "lololo"

    2. make a string, and return first half

    For example: "abcdef" will be "abcdef"

    3. make a string, first and second string lose their initial letter and combine them.

    For example : "Hi There" will be "ihere"

    4. last but not least, make a string that yield 2 middle characters

    For example : "String" will be "ri" or "Code" will be "od".

    Really appreciate, if anyone can explains and help me, I've just start learning Java.

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

    Default

    You really don't want to post your homework assignment here. Instead try to solve it as best you can and then ask a specific question over a specific point of misunderstanding.

  3. #3
    jimmy-lin is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    For the first part

    this is my code, the answer come out to be numbers instead of letters

    Java Code:
    public String extraEnd(String str)
        {
            String result = "";
            int len = str.length();
            
    
                String extra = str.substring(0, len);
                char lastChar = str.charAt(len - 1);
                char nextToLast = str.charAt(len - 2);
                String newStr1 = Integer.toString(lastChar);
                String newStr2 = Integer.toString(nextToLast);
                String newStr = newStr2 + newStr1;
        
                
            result = newStr + newStr + newStr;
             
            return result;
        }

  4. #4
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    What string are you using in this example? There shouldn't be any reason to use Integer.toString(). If you sent in something like 1234 as your string then of course it's going to come out as numbers instead of letters.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  5. #5
    toymachiner62 is offline Member
    Join Date
    Oct 2009
    Posts
    63
    Rep Power
    0

    Default

    Get rid of these lines

    String newStr1 = Integer.toString(lastChar);
    String newStr2 = Integer.toString(nextToLast);
    String newStr = newStr2 + newStr1;

    and change result to be

    result = nextToLast + lastChar;

  6. #6
    jimmy-lin is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    Hi again, I'm on the last part

    This is my code so far
    Java Code:
     public String middleTwo(String str)
        {
            String result = "";
            int len = str.length();
            
            if (len < 2)
            {
                return str; //this one take care of a string with 2 characters.
            }
            else
            {
            String firstmid = str.substring(len/2);
            char firstMid = str.charAt(len/2 - 1);
            char secondMid = str.charAt(len/2);
            
            result = firstmid + firstMid + secondMid;
            }
            return result;
            
        }
    I try to remove String first mid and make result = char firstMid + char secondMid, but i get error that I cannot convert int to String. When I input the String firstmid into the result so it would be result = firstmid + firstMid + secondMid; it run fine, but the answer will show off the 2nd of of the string.

    Here is the error that I'm getting
    Java Code:
     X middleTwo("string")-> expected:<[]ri> but was:<[ing]ri>
      X middleTwo("code")-> expected:<[]od> but was:<[de]od>
      X middleTwo("Practice")-> expected:<[]ct> but was:<[tice]ct>
      X middleTwo("ab")-> expected:<[]ab> but was:<[b]ab>
      X middleTwo("0123456789")-> expected:<[]45> but was:<[56789]45>

  7. #7
    jimmy-lin is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    nevermind, i figure it out. All i need to do is make

    String firstmid = str.substring(0, len - len);

Similar Threads

  1. beginner here...help please
    By shroomiin in forum New To Java
    Replies: 6
    Last Post: 09-16-2009, 12:06 AM
  2. How And Where To Start For A Beginner
    By DaneX in forum New To Java
    Replies: 3
    Last Post: 08-20-2009, 05:24 PM
  3. Beginner needs help!
    By Polyy in forum New To Java
    Replies: 1
    Last Post: 11-27-2008, 06:12 AM
  4. almost done...beginner needs help plz..
    By shongo in forum New To Java
    Replies: 15
    Last Post: 11-10-2008, 09:14 AM
  5. beginner needs help with OBD-II input
    By andrewos in forum New To Java
    Replies: 3
    Last Post: 07-30-2007, 10:46 AM

Posting Permissions

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