char input = scanner.nextLine().charAt(0);
First thing that's unclear is what Object type is scanner?
But for now I'll assume scanner is the Scanner object from Java.util.Scanner
If that's the case scanner.nextLine() actually returns a String.
String has a charAt() method that will allow you to pick out a character anywhere in the string.
However scanner.nextLine()
is getting the entire line, not just one word. So really scanner.nextLine().charAt(0)
is getting the first character in the line.
scanner.next()
will give you the next word in the line.
If the line contained "Hello World"
scanner.next().charAt(0)
would return the character 'H'.
the next call of scanner.next().charAt(0)
would then return the character 'W'
public static void main(String[] args) {
boolean finished = false;
ArrayList<Character> firstLetters = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
while (!finished) {
firstLetters.add(scanner.next().charAt(0));
}
}
The above code sample might give you the behavior you're looking for.
Please note that the while loop will run forever until finished
becomes true.
Your program will have to decide when to set finished
to true.
AND here's a couple of links about Java's Scanner class