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 8741001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:13:52+00:00 2026-06-13T11:13:52+00:00

I don’t know why I am struggling so badly with this but any help

  • 0

I don’t know why I am struggling so badly with this but any help would be much appreciated.

I am creating my own tokenizer that takes in a file with a list of commands, delimiters and values. It then outputs each “token” along with what type it is.

INPUT: AND 3, 4, 5 ; some comments

I need to output:

AND --- command
3 --- value
, --- delimiter
4 --- value
, --- delimiter
5 --- value

I have it working right now to where I am outputting:

AND 3, 4, 5 --- delimiter

but I need to break it down further.

Here is where I am at currently:

ArrayList<Token> tokenize(String[] input) {
    ArrayList<Token> tokens = new ArrayList<Token>();
    for (String str : input) {
        Token token = new Token(str.trim());
        //Check if int
        try{
            Integer.parseInt(str);
            token.type = "number";
        } catch(NumberFormatException e) {

        }
        if (token.type == null) {
            if (commands.contains(str))
                token.type = "command";
             else if (str.contains(",")) {
                token.type = "delimiter";
            } else if (destValues.contains(str))
                token.type = "destination";
            else
                token.type = "unknown";
        }

        if(! token.type.equals("unknown"))
            tokens.add(token);
    }
    return tokens;
}

Only real constraints I have with this assignment is not being able to use StringTokenizer and regex.

  • 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-13T11:13:53+00:00Added an answer on June 13, 2026 at 11:13 am

    Its seem s that your input is incorrect. Try this to split the input and then use your tokenize method.

    import java.util.*;
    
    public class Foo {
    
        public static void main( String[] args ) {
    
    
            String input = "AND 3,    4, 5   ; some comments";
            List<String> parts = new ArrayList<String>();
    
            // removing comments
            input = input.split( ";" )[0];
    
            // splits using spaces
            String[] firstPass = input.trim().split( " " );
    
            for ( String s : firstPass ) {
    
                // the current part cannot be empty
                if ( !s.trim().isEmpty() ) {
    
                    // splits using comma
                    String[] secondPass = s.split( "," );
    
                    for ( String ss : secondPass ) {
                        parts.add( ss.replace( ",", "" ) );
                    }
    
                    // verifies if the current part has a comma
                    // and if so, inserts it as a part
                    if ( s.contains( "," ) ) {
                        parts.add( "," );
                    }
    
                }
    
            }
    
            for ( String a : parts ) {
                System.out.println( a );
            }
    
        }
    
    }
    

    EDIT: As my first anwer worked, here is a complete example with some refactors…

    import java.util.*;
    
    public class MyTinyParser {
    
        private static final String COMMANDS = "AND OR FOO BAR";
    
        private List<String> extract( String input ) {
    
            List<String> parts = new ArrayList<String>();
    
            // removing comments
            input = input.split( ";" )[0];
    
            // splits using spaces
            String[] firstPass = input.trim().split( " " );
    
            for ( String s : firstPass ) {
    
                // the current part cannot be empty
                if ( !s.trim().isEmpty() ) {
    
                    // splits using comma
                    String[] secondPass = s.split( "," );
    
                    for ( String ss : secondPass ) {
                        parts.add( ss.replace( ",", "" ) );
                    }
    
                    // verifies if the current part has a comma
                    // and if so, inserts it as a part
                    if ( s.contains( "," ) ) {
                        parts.add( "," );
                    }
    
                }
    
            }
    
            return parts;
    
        }
    
        public List<Token> tokenize( String input ) {
    
            List<Token> tokens = new ArrayList<Token>();
    
            for ( String str : extract( input ) ) {
    
                Token token = new Token( str );
    
                // check if int
                try{
                    Integer.parseInt( str );
                    token.type = "number";
                } catch(NumberFormatException e) {
                }
    
                if ( token.type == null ) {
    
                    if ( COMMANDS.contains(str)){
                        token.type = "command";
                    } else if (str.contains(",")) {
                        token.type = "delimiter";
                    } else {
                        token.type = "unknown";
                    }
    
                }
    
                if( !token.type.equals( "unknown" ) ) {
                    tokens.add( token );
                }
    
            }
    
            return tokens;
    
        }
    
        private class Token {
    
            String value;
            String type;
    
            Token( String value ) {
                this.value = value;
            }
    
            @Override
            public String toString() {
                return String.format( "Token[%s, %s]", value, type );
            }
    
        }
    
        public static void main( String[] args ) {
    
            MyTinyParser mtp = new MyTinyParser();
            List<Token> tokens = mtp.tokenize( "AND 3,    4, 5   ; some comments" );
    
            for ( Token t : tokens ) {
                System.out.println( t );
            }
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(Don't know if this is strictly on-topic, but I don't see any better Stack
Don't know why but font is not displaying.Please help. CSS(in css folder): style.css: @font-face
I don't know why, but this code worked for me a month ago... maybe
Don't know if anyone can help me with this or if it's even possible.
I don't know if this question is trivial or not. But after a couple
Don't know if this is the right place to ask this, but I will
Don't know why this is happening, but after submitting a form via JS (using
Don't know if this is an eclipse specific problem but whenever I declare a
Don't know if I'm over-thinking this or not.. but I'm trying to be able
Don't know why but I can't find a solution to this. I have 3

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.