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: string method

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

    Default string method

    Hi there,

    I am new to java and need help with my assignments.

    I am trying to write method for string but unable to work out how to get the return value.

     
    public String getPartFrom(String full, String start, String last)
    {
    // need to return word between String start and String last but unable to invoke how :-s
     
    }

    Can someone please help me.

    Many thanks
    dstha


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,266
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: string method

    Read the API for String, in particular the substring function

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

    Default Re: string method

    Thanks for the reply but still cannot figure out how to make it work.. tried everything
    public String getPartFrom(String full, String start, String last)
    {
    string.getPartFrom(start, last);
    }

    tried
    public String getPartFrom(String full, String start, String last)
    {
    return.getPartFrom(start, last);
    }

    cannot compile the code
    Last edited by helloworld922; February 25th, 2010 at 07:05 PM.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: string method

    What is it the method should do? Are you trying to find the stuff between the String start and String last? Or are start and last integer indexes to where you want to take your substring from?

    A few pointers on why your code didn't compile:

    Java is a case-sensitive language. Thus, String != string.
    getPartFrom() is not a defined method of Java's String class. Instead, Java has a substring() method that will take the substring between two indices. Also, this method is not static so you must reference an object to call it (as opposed to using String., use var_name., where var_name is the name of your variable).

    String hello = "Hello World!";
    System.out.println("The first five letters of the string are " + hello.substring(0,5));

    In your second attempt, you try to use return.. It should simply be return [space] value; ([space] means any white space separation).

    ex.

    public static int value()
    {
         return 5;   // return the value 5
    }

  5. The Following User Says Thank You to helloworld922 For This Useful Post:

    dstha (March 5th, 2010)

Similar Threads

  1. How to return sub string from String
    By humdinger in forum Collections and Generics
    Replies: 1
    Last Post: February 14th, 2010, 10:16 AM
  2. Replies: 1
    Last Post: February 4th, 2010, 03:23 PM
  3. Replies: 2
    Last Post: November 3rd, 2009, 05:28 AM
  4. how to reuse method returned string ??
    By zeeshanmirza in forum Collections and Generics
    Replies: 7
    Last Post: September 4th, 2009, 03:54 PM