Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7904541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:07:22+00:00 2026-06-03T10:07:22+00:00

hi guys im beginner in java :) so what im doing is that im

  • 0

hi guys im beginner in java 🙂 so what im doing is that im trying to build a lexical analyzer using java but im stuck trying to read the next character in the string i tried to use the lookup function to check but i dont know how to use it in the main. this is the code

 import java.util.*;
 import java.util.Scanner;

 public class LAnalyze{

public static int i;

public static void main(String[] args) {

    String s = "(3+4)*5";
    System.out.println("s = " + s);

    char[] chars = s.toCharArray();

    for (char i = 0; i < 8; i++) {
        if (i == 0) {
            i = s.charAt(0);
            lookup();

        }
    }
}

public int lookup() {

    switch (i) {
    case '(':
        System.out.println("Next Token = " + "'" + i + "'"
                + " Next lexeme = 25");
        break;
    case ')':
        System.out.println("Next Token = " + "'" + i + "'"
                + " Next lexeme = 26");
        break;
    case '+':
        System.out.println("Next Token = " + "'" + i + "'"
                + " Next lexeme = 21");
        break;
    case '-':
        System.out.println("Next Token = " + "'" + i + "'"
                + " Next lexeme = 22");
        break;
    case '*':
        System.out.println("Next Token = " + "'" + i + "'"
                + " Next lexeme = 23");
        break;
    case '/':
        System.out.println("Next Token = " + "'" + i + "'"
                + " Next lexeme = 24");
    default:
        System.out.println("Next Token = " + "'" + i + "'"
                + " Next lexeme = 20");
    }
    return 0;
}
 }



   /* the output is suppose to be something like this:

    Next Token = "(" Next lexeme = 25
    Next Token = "3" Next lexeme = 20
    Next Token = "+" Next lexeme = 21
    .
    .
    .
    Next Token = ")" Next lexeme = 26
     */
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T10:07:31+00:00Added an answer on June 3, 2026 at 10:07 am

    You can do by the following code.

    public static void main(String[] args) {
    
            String s = "(3+4)*5";
            System.out.println("s = " + s);
    
            char[] chars = s.toCharArray();
    
            for (int i = 0; i < chars.length ; i++) {
                //System.out.println("s = " + s);
                lookup(chars[i]);
            }
        }
    
        public static int lookup(int i) {
    
            switch (i) {
            case '(':
                System.out.println("Next Token = " + "'" + (char)i + "'"
                        + " Next lexeme = 25");
                break;
            case ')':
                System.out.println("Next Token = " + "'" + (char)i + "'"
                        + " Next lexeme = 26");
                break;
            case '+':
                System.out.println("Next Token = " + "'" + (char)i + "'"
                        + " Next lexeme = 21");
                break;
            case '-':
                System.out.println("Next Token = " + "'" + (char)i + "'"
                        + " Next lexeme = 22");
                break;
            case '*':
                System.out.println("Next Token = " + "'" + (char)i + "'"
                        + " Next lexeme = 23");
                break;
            case '/':
                System.out.println("Next Token = " + "'" + (char)i + "'"
                        + " Next lexeme = 24");
            default:
                System.out.println("Next Token = " + "'" + (char)i + "'"
                        + " Next lexeme = 20");
            }
            return 0;
        }
    

    OUTPUT

    s = (3+4)*5
    Next Token = '(' Next lexeme = 25
    Next Token = '3' Next lexeme = 20
    Next Token = '+' Next lexeme = 21
    Next Token = '4' Next lexeme = 20
    Next Token = ')' Next lexeme = 26
    Next Token = '*' Next lexeme = 23
    Next Token = '5' Next lexeme = 20
    

    Hope this will help you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hey guys, beginner here. I have written a program that outputs files to .txt's
Guys, I have a CSS Layout that I am using that has a header,
I'm a beginner java programmer. I'm working on an application that decrypts some data.
Thank you for viewing. I'm a beginner with Java swing. I'm trying to merge
Ok guys, i'm very beginner and trying to enter string to a char array
Possible Duplicate: URL rewriting - Beginners guide Hey Guys I've been seeing sites using
Guys how can i remove an input with a specific value using jquery? I
Guys. I use getpixel & getdata to retrieve data from the same pic.but some
guys. I'm working on some audio services on iOS. I trying to search any
Beginner programmer here....hope it makes sense :) I have created a console app that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.