0

I've tried numerous times with different regexes but none seem to work. Basically, I have a StringTokenizer that is currently splitting the string by spaces, but I want it to split by each character instead. My current code:

FileReader fr = new FileReader("test.txt");
BufferedReader br = new BufferedReader(fr);
StringTokenizer st = new StringTokenizer(br.readLine());
Vonais
  • 17
  • 4

3 Answers3

3
char c = reader.next().charAt(0);

This gets a string's character at index zero. Strings begin at index zero. Also, an alternative is

for (int i = 0; i < numberOfChars; ++i){
    // get the character using charAt(i)
    // store the character using an array, as others have specified
}

Remember to use new Scanner(BufferedReader(FileReader("test.txt")));

For more info, read Take a char input from the Scanner

Community
  • 1
  • 1
Don Larynx
  • 667
  • 5
  • 15
2

I have a StringTokenizer that is currently splitting the string by spaces,

Read the StringTokenizer API. If you don't specify the delimiters, then the default delimeters are used.

So you need to specify all the delimeters.

Or, and easier approach is to just use the String.toCharArray() method and iterate through the array.

camickr
  • 308,339
  • 18
  • 152
  • 272
2

If you need them as chars:

char[] characters = br.readLine().toCharArray();

If you need them as Strings:

String[] characters = br.readLine().split("");
Andres
  • 51
  • 1
  • 5