You can use string.indexOf('a').

If the char a is present in string :

it returns the the index of the first occurrence of the character in the character sequence represented by this object, or -1 if the character does not occur.

Answer from mP. on Stack Overflow
Discussions

Find all index of occurrences of character in a string
Why would you even need the indexOf method? The naive, straightforward approach is to use .charAt and to loop over the characters in the string using a forloop. (TBH, .indexOf does exactly the same under the hood, just adds a starting index and it stops on the first occurrence). Honestly, the only way to learn proper programming is to stop searching for solutions and to sit down and try to work out your own solutions, because otherwise, you get in over your head, as in your case ending with code that you might have been able to somewhat reproduce, but that you don't understand. Start with pen(cil) and paper and try to figure out how you, the person, would address a problem. Only once you have found a solution start thinking about programming it. And don't memorize. Especially not code. Code adapts to the situation and is only the end product, not the starting point. Edit: Very mature move/s of you blocking people that offer help and advice to you just because you feel personally attacked or disagree with what has been said. More on reddit.com
๐ŸŒ r/learnjava
10
3
October 7, 2024
[JAVA] Best practice to obtain substring using index of a character that may not be in original string.
Step through the string, one character at a time, looking for a comma. If you reach either the end of the string or a comma, you know what the first id is. More on reddit.com
๐ŸŒ r/learnprogramming
7
1
April 10, 2015
[homework java] I don't know how to replace string using the indexOf method
This should help you better understand indexOf http://codingbat.com/doc/java-string-indexof-parsing.html More on reddit.com
๐ŸŒ r/learnprogramming
10
0
October 9, 2017
Java why i cant check if string contains char?
i can do if(test[x].contains(โ€œcharโ€); โ€œcharโ€ is a String, not a char. Contains presumably expects a string. If you try to call it with a char, it will fail. Edit: actually it expects a CharSequence. String implements CharSequence, but char does not. More on reddit.com
๐ŸŒ r/learnprogramming
3
2
March 30, 2019
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ searching-for-characters-and-substring-in-a-string-in-java
Searching For Characters and Substring in a String in Java - GeeksforGeeks
July 23, 2025 - Its parameters specify the sequence of characters to be searched and throw NullPointerException if seq is null. ... Note: CharSequence is an interface that is implemented by String class, Therefore we use string as an argument in contains() method.
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ tutorial โ€บ java โ€บ data โ€บ manipstrings.html
Manipulating Characters in a String (The Javaโ„ข Tutorials > Learning the Java Language > Numbers and Strings)
See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases. See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases. The String class has a number of methods for examining the contents of strings, finding characters or substrings within a string, changing case, and other tasks.
๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ strings in java โ€บ java string indexof()
Java String indexOf()
December 26, 2024 - Found 'a' at position: 1 Found ... programming language. Java is widely used in web development" and then used the indexOf() method to find all occurrences of the character 'a' or the substring "Java" in the str...
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ lang โ€บ String.html
String (Java Platform SE 8 )
July 21, 2026 - The class String includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase. Case mapping is based on the Unicode Standard version specified by the Character class. The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings.
Find elsewhere
๐ŸŒ
Medium
medium.com โ€บ @AlexanderObregon โ€บ finding-the-middle-character-of-a-word-in-java-strings-8b5a91bd5a0c
Finding the Middle Character of a Word in Java Strings
August 16, 2025 - Learn how to find the middle character in a Java string, handle odd or even lengths, and see the mechanics of charAt and substring in action.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjava โ€บ find all index of occurrences of character in a string
r/learnjava on Reddit: Find all index of occurrences of character in a string
October 7, 2024 -

https://books.google.com.np/books?id=h0Uhz4lBoF8C&pg=PA172&dq=find+index+of++occurrences+character+in+a+string&hl=en&newbks=1&newbks_redir=0&sa=X&ved=2ahUKEwjI-qzhp_uIAxX5S2wGHUHiL_MQ6AF6BAgGEAI#v=onepage&q&f=false

I found this solution here and re-wrote my code for it. And it works. And I was also attempting to use similar logic. However, I don't really grasp it by heart till of now. I am not able to internalize it.

if (wordToGuess.contains(userGuess)) {
                int aIndex = -1;
                while ((aIndex = wordToGuess.indexOf(userGuess, ++aIndex)) > -1) {
                    indexArr[aIndex] = 1;
                }
                displayAsterik(indexArr, wordToGuess);
            } else {
                mistakes = mistakes + 1;
            }

This is the code, the while loop is the part I am failing to analyze. I can obviously trace this loop with each input and see it works. However, the bigger question is designing such solutions in which cases in the future.

Top answer
1 of 5
7
Why would you even need the indexOf method? The naive, straightforward approach is to use .charAt and to loop over the characters in the string using a forloop. (TBH, .indexOf does exactly the same under the hood, just adds a starting index and it stops on the first occurrence). Honestly, the only way to learn proper programming is to stop searching for solutions and to sit down and try to work out your own solutions, because otherwise, you get in over your head, as in your case ending with code that you might have been able to somewhat reproduce, but that you don't understand. Start with pen(cil) and paper and try to figure out how you, the person, would address a problem. Only once you have found a solution start thinking about programming it. And don't memorize. Especially not code. Code adapts to the situation and is only the end product, not the starting point. Edit: Very mature move/s of you blocking people that offer help and advice to you just because you feel personally attacked or disagree with what has been said.
2 of 5
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full - best also formatted as code block You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ indexof-in-java-how-to-find-the-index-of-a-string-in-java
indexOf in Java โ€“ How to Find the Index of a String in Java
March 24, 2022 - The indexOf method returns the ... in a string. In this article, we'll see the syntax for the different indexOf methods. We'll also look at some examples to help you understand and use them effectively to find the index of a character or substring ...
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ java โ€บ standard-library โ€บ java โ€บ lang โ€บ String โ€บ indexOf
Java String indexOf() - Find Character Index | Vultr Docs
December 17, 2024 - Explore how this method can help ... practical implementation examples. Start with a simple string. Use indexOf() to find the position of a character....
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 370494 โ€บ java โ€บ Finding-character-String
Finding a character in a String (Java in General forum at Coderanch)
In more complicated cases, the regular expression support in JDK 1.4 (including several new methods in String itself) will help you. Writing your own character-munching code should be strictly the last resort. - Peter ยท Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd ... How come StringTokenizer doesn't work?
๐ŸŒ
Quora
quora.com โ€บ How-can-I-find-a-particular-text-in-a-string-in-Java
How to find a particular text in a string in Java - Quora
Answer (1 of 2): We can use indexOf method to find particular text or alphabet from given String. Scanner sc = new Scanner(system.in); System.out.println("Enter String"); String str = sc.nextLine(); System.out.println("Enter text"); String find = sc.nextLine(); int index = str.indexOf(find)...
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ java-string-substring
Master Java Substring Method: Examples, Syntax, and Use Cases | DigitalOcean
Learn how to use the Java substring method. Explore syntax, practical examples, and common errors to handle substrings effectively.
๐ŸŒ
DaniWeb
daniweb.com โ€บ programming โ€บ software-development โ€บ threads โ€บ 33657 โ€บ finding-the-first-number-char-in-a-string
java - Finding the first number char in a string | DaniWeb
October 10, 2005 - If you get any number greater than ... a single character) you look for exists in the target string, couldn't be easier. ... You do understand that the regular expression package has a standard system for doing just that? It's already there and far more flexible than any rigid function to do a single small thing can provide. After all, as I said, there's tons of small things someone somewhere may find ...
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ getting a character by index from a string in java
Getting a Character by Index From a String in Java | Baeldung
November 24, 2025 - As the example shows, we can use getChars() to efficiently extract a single character or a subset of characters from an input String. Additionally, with Java 8, we can leverage the Streams API to process Strings in a functional style.
๐ŸŒ
Sabe
sabe.io โ€บ blog โ€บ java-check-string-contains-character
How to Check if a String Contains a Character in Java | Sabe
May 25, 2022 - You can either use the contains() method or the indexOf() method to check for the presence of a character in a string. Hopefully, this has been helpful to you and thanks for reading!
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-program-to-get-a-character-from-a-string
Java Program to Get a Character from a String - GeeksforGeeks
3 weeks ago - Given a string and an index, we can retrieve the character present at that specific position. Java provides several methods for accessing characters, with charAt() being the simplest and most commonly used approach.
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_ref_string.asp
Java String Reference
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ... The String class has a set of built-in methods that you can use on strings.