I want to print a letter instead of the index position using the indexOf(); method. The requirement is that: Inputs a second string from the user. Outputs the character after t
You can access a single character, or a letter, by caling método charAt()
from String
class
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String phrase = keyboard.nextLine();
char firstLetter = phrase.charAt(0);
System.out.println("First Letter : " + firstLetter);
}
So, running this code, assuming the input is StackOverFlow
, the output will be S
In your code I think doing the follow will work:
String letter = keyboard.next();
first = letter.charAt(0);
That might help!