0
Scanner m = new Scanner(new File("C:\\Map.txt));
System.out.println(m.next());

say the Map.txt has the line fgdfgdfgfgfdg in it.

when i try to use it, it prints the whole line instead of one token/letter at a time. How can I read only 1 letter?

P.S - I'm aware the file has to be in a try-catch block, I ommitted that here and I also tried using FileReader instead of File, same thing happens.

Zod
  • 153
  • 1
  • 9

1 Answers1

0

m.next() takes the whole word, that is every letter until the next whitespace. You can do something like this:

Scanner m = new Scanner(...);
System.out.println(m.next().charAt(0));

Edit: For reading one char at a time you would then store the word as a string and loop through the chars

Scanner m = new Scanner(...);
String word = m.next();

for (int i = 0; i < word.length(); i++) {
    System.out.println(word.charAt(i));
}
Einar
  • 896
  • 2
  • 9
  • 27