Hello everyone,

I am currently taking java and Im trying to learn how to write methods. I want to know learn the best way that I can learn how to read/write methods in a code. Im currently writing a code using lastindexOf. I do understand that im using this to (lets say i have and array)find the last object of the same in that array, but I don't understand how to write this. Please help me too understand what Im doing!!! thanks alot and in advance I do appreciate any help anyone can give me.

Recommended Answers

All 3 Replies

Well, let's see what you've tried.

Hello everyone,

I am currently taking java and Im trying to learn how to write methods. I want to know learn the best way that I can learn how to read/write methods in a code. Im currently writing a code using lastindexOf. I do understand that im using this to (lets say i have and array)find the last object of the same in that array, but I don't understand how to write this. Please help me too understand what Im doing!!! thanks alot and in advance I do appreciate any help anyone can give me.

lastIndexOf( ) Searches for the last occurrence of a character or substring.


You can specify a starting point for the search using these forms:

int lastIndexOf(String str, int startIndex)

Here, str specifies the substring.

check out this example:

public class IllustrateLastIndexOfString {
  public static void main(String args[]) 
{
    String prabhu = "o pailon haari", bholenaath = "";
    // double quotes as arguments inside parentheses
    // Method returns last index of letter or word passes in single ang
    System.out.println("last index of letter 'i':  "
        + prabhu.lastIndexOf('i'));

    bholenaath = "bam bolo bam bolo bam bam bam";
    // here instead of index of word 'bolo', the letter which word stats
    // that is 'b', is returned and same happens every time for every word
    // or statement
    System.out.println("Index of letter 'b' is returned:  "
        + bholenaath.lastIndexOf("bolo"));
    // here were complete statement is passed in the arguments but still
    // method returns the index of first letter only
    System.out.println("Index of letter 'o' is returned:  "
        + prabhu.lastIndexOf("o pailon haari"));
  }
}

My results are as below:

last index of letter 'i': 13
Index of letter 'b' is returned: 13
Index of letter 'o' is returned: 0

Hope this gets you up to some speed!

No you don't use the lastindexOf I know with arrays. When you say you know how to use that method, which class does this method belong to. Are you referring to the method of the String class?
Better explain your problem.

As for writing and calling methods there are plenty of tutorials you can read and we are not here to repeat those tutorials to you. You can learn those things on your own. Just study your notes on how to declare methods.

But we are here to help with the logic, so explain what your method should do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.