File

Set whitespaceChars example

With this example we are going to demonstrate how to use the whitespaceChars(int low, int hi) method of a StreamTokenizer. This method specifies that all characters c in the range low <= c <= high are white space characters. In short, to use the whitespaceChars(int low, int hi) method you should:

  • Create a new FileReader.
  • Create a new BufferedReader using the fileReader.
  • Create a new StreamTokenizer that parses the given bufferedReader.
  • Use the whitespaceChars(int low, int hi) method of a StreamTokenizer.
  • Iterate over the tokens of the tokenizer. For every token, check the type of the token, using ttype method of StreamTokenizer. According to the type of the token print its value.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.StreamTokenizer;

public class Main {

    public static void main(String args[]) throws Exception {

  FileReader fileReader = new FileReader("C:/Users/nikos7/Desktop/output.txt");

  BufferedReader bufferedReader = new BufferedReader(fileReader);

  StreamTokenizer tokenizer = new StreamTokenizer(bufferedReader);

  tokenizer.whitespaceChars(',', ',');
  while (tokenizer.nextToken() != StreamTokenizer.TT_EOF) {
switch (tokenizer.ttype) {
    case StreamTokenizer.TT_WORD:
  System.out.println(tokenizer.lineno() + ") " + tokenizer.sval);
  break;
    case StreamTokenizer.TT_NUMBER:
  System.out.println(tokenizer.lineno() + ") " + tokenizer.nval);
  break;
    default:
  System.out.println(tokenizer.lineno() + ") " + (char) tokenizer.ttype);
}
  }
  fileReader.close();
    }
}

Output:

.
.
.
351) render
351) real
351) world
351) terrain
351) from
351) heightmap
351) using
351) open
351) data
.
.
.

 
This was an example of how to use the whitespaceChars(int low, int hi) method of StreamTokenizer in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button