The method you're looking for is charAt. Here's an example:
String text = "foo";
char charAtZero = text.charAt(0);
System.out.println(charAtZero); // Prints f
For more information, see the Java documentation on String.charAt. If you want another simple tutorial, this one or this one.
If you don't want the result as a char data type, but rather as a string, you would use the Character.toString method:
String text = "foo";
String letter = Character.toString(text.charAt(0));
System.out.println(letter); // Prints f
If you want more information on the Character class and the toString method, I pulled my info from the documentation on Character.toString.
The method you're looking for is charAt. Here's an example:
String text = "foo";
char charAtZero = text.charAt(0);
System.out.println(charAtZero); // Prints f
For more information, see the Java documentation on String.charAt. If you want another simple tutorial, this one or this one.
If you don't want the result as a char data type, but rather as a string, you would use the Character.toString method:
String text = "foo";
String letter = Character.toString(text.charAt(0));
System.out.println(letter); // Prints f
If you want more information on the Character class and the toString method, I pulled my info from the documentation on Character.toString.
You want .charAt()
Here's a tutorial
"mystring".charAt(2)
returns s
If you're hellbent on having a string there are a couple of ways to convert a char to a string:
String mychar = Character.toString("mystring".charAt(2));
Or
String mychar = ""+"mystring".charAt(2);
Or even
String mychar = String.valueOf("mystring".charAt(2));
For example.
In Java How Do You Assign A Char To A Particular Index In A String?
Find all index of occurrences of character in a string
Java, how to count the indexes of a String array readin by Scanner?
[JAVA] Best practice to obtain substring using index of a character that may not be in original string.
Videos
So I just tried this in Java:
codeString.charAt(2) = 'x';
And it gave me an error '' Variable expected ''.
I don't understand? I wanted to target the 2nd index of that string called ' codeString ', and then reassign that position with ' x '. So what whatever was in that index will now be replaced with x.
What is wrong?
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.