how to solve the indexing in java programming language

Maseeh.Niazai
public static int indexOf(String text , char index){

        char array [] = new char[text.length()];
        for(int i = 0 ; i < text.length(); i++){
            array[i] = text.charAt(i);
        }// end of first loop
        // the above loop converts the string obj to char array

        for(int i = 0 ; i < array.length; i++){
            if(array[i] == index){ // if a given letter is exist in string 
                System.out.println("The index of " + index + " is " + i);
                return i; // show or return the index of that given letter
            }
        }//end of second loop 
        System.out.println("The Letter you Entered does not Exist");
        return -1;
    }// end of method 

this piece of code is for finding the index of string ; first it takes a string and a character as an input than it convert it to array of characters than it search for an given lettter if it finds it. method will return it's index but the main problem is that we have more than one same letter in a string so it will return the first one . and how could i detect the second one or how I can differentiate the second same letter and show it is index for example:

indexOf("kingJoker",'k');

here in kingjoker string we have two k letter the method can't find second k index so that is the problem.

Sergey Kalinichenko
  • Change the return type of your method to int[]
  • Walk the string once to count the matches
  • Make an array of the size equal to the count
  • Walk the string again, populating the return indexes as you go.

Here is your modified implementation:

public static int[] indexesOf(String text , char ch) {
    int count = 0;
    for (int i = 0 ; i != text.length() ; i++) {
        if (text.charAt(i) == ch) {
            count++;
        }
    }
    int[] indexes = new int[count];
    int pos = 0;
    for (int i = 0 ; i != text.length() ; i++) {
        if (text.charAt(i) == ch) {
            System.out.println("Found '" + ch + "' at " + i);
            indexes[pos++] = i;
        }
    }
    return indexes;
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

R: How to solve the following linear programming problem

分類Dev

How is standard library for programming language implemented?

分類Dev

How to set the programming language in eclipse's editor?

分類Dev

How we can read a JSON file with Go Programming Language?

分類Dev

How to solve java.lang.IllegalStateException?

分類Dev

Logical indexing in JAVA

分類Dev

How indexing works in Pandas?

分類Dev

How to get localport using java socket programming

分類Dev

How to solve an exception java.sql.SQLException: No suitable driver found?

分類Dev

How list indexing works in Jinja

分類Dev

How to transfer a zip file through socket programming in java?

分類Dev

go programming language bracket pair style

分類Dev

Get nth character of a string in Swift programming language

分類Dev

A programming language with garbage collection AND manual memory management

分類Dev

Documentation for multi-programming-language API

分類Dev

Mega API using python programming language

分類Dev

What is a programming language called if it is not interpreted or compiled

分類Dev

How to disable or remove specific language(java) from notepad++.?

分類Dev

How to convert an "ArrayList<ClassNod>" written in Java into a C++ program language?

分類Dev

How to specify language in addPrompt() in Rivr Java VoiceXML library

分類Dev

How to solve such a ratio function?

分類Dev

ElasticSearch - How does sharding affect indexing performance?

分類Dev

How this numpy advance indexing code works?

分類Dev

How does the indexing of the box in this gltf file work?

分類Dev

How to deal with indexing involving three vectors?

分類Dev

How to rebuild the 'indexing' package on MAC OSX

分類Dev

Java Programming Exercises?

分類Dev

Java/UML Programming

分類Dev

How to solve java.util.zip.ZipException duplicate entry: com/google/gson/annotations/Expose.class?

Related 関連記事

  1. 1

    R: How to solve the following linear programming problem

  2. 2

    How is standard library for programming language implemented?

  3. 3

    How to set the programming language in eclipse's editor?

  4. 4

    How we can read a JSON file with Go Programming Language?

  5. 5

    How to solve java.lang.IllegalStateException?

  6. 6

    Logical indexing in JAVA

  7. 7

    How indexing works in Pandas?

  8. 8

    How to get localport using java socket programming

  9. 9

    How to solve an exception java.sql.SQLException: No suitable driver found?

  10. 10

    How list indexing works in Jinja

  11. 11

    How to transfer a zip file through socket programming in java?

  12. 12

    go programming language bracket pair style

  13. 13

    Get nth character of a string in Swift programming language

  14. 14

    A programming language with garbage collection AND manual memory management

  15. 15

    Documentation for multi-programming-language API

  16. 16

    Mega API using python programming language

  17. 17

    What is a programming language called if it is not interpreted or compiled

  18. 18

    How to disable or remove specific language(java) from notepad++.?

  19. 19

    How to convert an "ArrayList<ClassNod>" written in Java into a C++ program language?

  20. 20

    How to specify language in addPrompt() in Rivr Java VoiceXML library

  21. 21

    How to solve such a ratio function?

  22. 22

    ElasticSearch - How does sharding affect indexing performance?

  23. 23

    How this numpy advance indexing code works?

  24. 24

    How does the indexing of the box in this gltf file work?

  25. 25

    How to deal with indexing involving three vectors?

  26. 26

    How to rebuild the 'indexing' package on MAC OSX

  27. 27

    Java Programming Exercises?

  28. 28

    Java/UML Programming

  29. 29

    How to solve java.util.zip.ZipException duplicate entry: com/google/gson/annotations/Expose.class?

ホットタグ

アーカイブ