List of Topics:
Research Breakthrough Possible @S-Logix pro@slogix.in

Office Address

Social List

How to Split the String in Java Using StringTokenizer?

StringTokenizer Example

Condition for Splitting a String in Java Using StringTokenizer

  • Description: In Java, the StringTokenizer class is used to split a string into tokens, which are substrings separated by delimiters. To use StringTokenizer, an instance is created by passing the string to be tokenized and the delimiter(s) as arguments to the constructor. By default, StringTokenizer splits the string based on whitespace characters (spaces, tabs, and newlines). However, you can specify custom delimiters (such as commas, semicolons, or any other character) by passing them as the second argument. The hasMoreTokens() method is used to check if more tokens are available, and the nextToken() method retrieves the next token from the string.
Sample Source Code
  • # Tokenizer.java
    package JavaSamples;

    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.util.StringTokenizer;
    import javax.swing.JOptionPane;

    public class Tokenizer extends javax.swing.JFrame {
    public Tokenizer() {
    initComponents();
    }

    @SuppressWarnings("unchecked")
    private void initComponents() {
    // UI setup code
    }
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    javax.swing.JFileChooser fileChooser = new javax.swing.JFileChooser();
    fileChooser.setDialogTitle("Choose a file");
    int result = fileChooser.showOpenDialog(this);
    if (result == javax.swing.JFileChooser.APPROVE_OPTION) {
    File selectedFile = fileChooser.getSelectedFile();
    jTextField1.setText(selectedFile.getAbsolutePath());
    } else {
    jTextField1.setText("No File Selected ");
    }
    }
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    String filePath = jTextField1.getText();
    String input = jTextArea1.getText();
    try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
    StringTokenizer tokenizer = new StringTokenizer(input, ",");
    while (tokenizer.hasMoreTokens()) {
    String token = tokenizer.nextToken();
    writer.write(token);
    writer.newLine();
    JOptionPane.showMessageDialog(null, "Split the String successfully");
    }
    } catch (Exception e) {
    System.out.println(e.getMessage());
    }
    }
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(() -> {
    new Tokenizer().setVisible(true);
    });
    }
    }
Screenshots
  • STEP 1: The user selects a text document from local files.
  • Select File Dialog

  • STEP 2: The file path will be displayed in the text field after the file is selected.
  • Display File Path

  • STEP 3: The user types the text in the text area and clicks the "Split" button to divide the string into tokens.
  • Split Text

  • STEP 4: The content is written to the file after splitting into tokens, displayed as individual lines.
  • Split File Output

Related Links