Java Program to Analyze Tokens in an Input String

How can we write a Java program to analyze tokens in an input string?

Sample run: Input: A + B - C / D Output: TOKEN#1 A Identifier TOKEN#2 + Operator TOKEN#3 B Identifier TOKEN#4 - Operator TOKEN#5 C Identifier TOKEN#6 / Operator TOKEN#7 D Identifier Total number of Tokens: 7, Final answer:

Java Program to Analyze Tokens:

To write a Java program to accept and process an input string, you can use the Scanner class to read the input from the user and the split method of the String class to split the input string into tokens.

Explanation:

To create a Java program that analyzes tokens in an input string, you can utilize the Scanner class to read user input and the split method of the String class to split the input string into tokens using a specified delimiter.

By iterating over the obtained tokens, you can determine the attributes of each token, such as whether it is an identifier (A, B, C, or D) or an operator (+, -, or /).

Here is an example Java program that accomplishes this task:

import java.util.Scanner; public class TokenAnalyzer {   public static void main(String[] args) {     Scanner scanner = new Scanner(System.in);     System.out.print("Enter an input string: ");     String input = scanner.nextLine();     String[] tokens = input.split(" ");     System.out.println("Total number of Tokens: " + tokens.length);     for (int i = 0; i < tokens.length; i++) {       if (tokens[i].matches("[A-D]")) {         System.out.println("TOKEN#" + (i + 1) + " " + tokens[i] + " Identifier");       } else if (tokens[i].matches("[+\\-/]")) {         System.out.println("TOKEN#" + (i + 1) + " " + tokens[i] + " Operator");       }     }   } }

This program uses regular expressions to check if a token matches the pattern of an identifier (A, B, C, or D) or an operator (+, -, or /). The matches method returns true if the token matches the pattern, and false otherwise.

← Chy anne s rectangular garden tasks Best practices for variable declaration in programming languages →