How to read an input file char by char using a Scanner?
input, io, java
Solution
You can convert in an array of chars.
import java.io.*;
import java.util.Scanner;
public class ScanXan {
public static void main(String[] args) throws IOException {
Scanner s = null;
try {
s = new Scanner(new BufferedReader(new FileReader("yourFile.txt")));
while (s.hasNext())
{
String str = s.next();
char[] myChar = str.toCharArray();
// do something
}
} finally {
if (s != null) {
s.close();
}
}
}
Problem
I have to use `Scanner`, so is there a `nextChar()` instead of `nextLine()` method that I could use? Thanks!