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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:34:18+00:00 2026-06-19T00:34:18+00:00

This is a custom built parser, because I really dislike the way the default

  • 0

This is a custom built parser, because I really dislike the way the default java.util.Scanner works.

My problem is that when I create the parser using new Parser(“Parsed phrase here”) or the function reloadBuffer(“Parsed phrase here”) it misses the last word of the input. I tried to make this code as readable as I could, but it’s still pretty dense, sorry for that. Oh, and if this ever gets fixed, feel free to use it.

import java.util.*;

public class Parser
{

    public static char letters[] = new char[]{'q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l','z','x','c','v','b','n','m','Q','W','E','R','T','Y','U','I','O','P','A','S','D','F','G','H','J','K','L','Z','X','C','V','B','N','M'};
    public static char numbers[] = new char[]{'0','1','2','3','4','5','6','7','8','9'};

    public ArrayList<String> words;
    public String buffer;
    public int wordIndex;
    /**
     * Assembles an empty parser.
     */
    public Parser()
    {
        words = new ArrayList<String>();
        buffer = "";
        wordIndex = 0;
    }
    /**
     * Assembles the Parser with given String as input. Uses reloadBuffer();
     * @see reloadBuffer
     */
    public Parser(String input)
    {
        words = new ArrayList<String>();
        reloadBuffer(input);
    }
    /**
     * Parses each word/set of chars into the array. Must be called before any retreiving of words can be done. 
     * This is basically the core function of the class. Be very careful if you edit this part.
     */
    public void parseBuffer(){
        String input = buffer;
        while(input.length()>=1)
        {
            input = trimToSelectedChars(input);
            words.add(removeFirstSet(input)[0]);
            input = removeFirstSet(input)[1];
        }
    }
    /**
     * Resets the array with given String as input. Used in the primary constructor. Uses parseBuffer();
     * @see parseBuffer()
     */
    public void reloadBuffer(String input){
        buffer = input;
        wordIndex = 0;
        parseBuffer();
    }

    /**
     * @return the next word parsed from the string, based upon the value of wordIndex.
     */
    public String next(){
        wordIndex++;
        if (wordIndex<= words.size()+1){
            try {return words.get(wordIndex-1);
            } catch(Exception ex) {
                System.err.println("Error: reached end of list. Resetting index to 0.");
                resetIndex();
            }
        }
        return "";
    }

    //Notice that when using wordAt(), it leaves the index where you selected, and it does not revert to where it was before.
    /**
     * @return the word at indicated index, much like the charAt() function, using the next() function. Also sets wordIndex to input index.
     * @see String
     * @see next()
     */
    public String wordAt(int index){
        wordIndex = index;
        return next();
    }

    /**
     * @return the first word parsed from the input.
     * @see String
     */
    public String firstWord()

    {
        return wordAt(0);
    }

    /**
     *Be careful in using lastWord() as it sets the wordIndex to the last value which will return a String
     *@return the last parsed word from the input.
     */
    public String lastWord(){
        return wordAt(words.size()-1);
    }

    /**
     * Resets the wordIndex to 0, the beginning.
     */
    public void resetIndex(){wordIndex = 0;}

    /**
     * return whether or not there is another word in the parser list.
     */
    public boolean hasNext(){
        return (wordIndex<words.size());
    }
    //internal methods here.
    private String[] removeFirstSet(String input)
    //removes the first set of adjecent letters from a string, and returns it.
    {
        String[] words = new String[2];
        int index = 0;
        if(input.length()<1) words[0] = "";
        while(index<input.length()){
            //this loop to retrieve the first word.
            if(isLetter(input.charAt(index))||isNumber(input.charAt(index))){
                index++; //if the first char is a letter, move on to the next one.
            }
            else{
                words[0]=input.substring(0,index);
                words[1]=input.substring(index);
                return words;
            }
        }
        return new String[]{"",""};
    }

    private String trimToSelectedChars(String input)
    //trims anything that is not a letter from the front of a String.
    {
        input = input.trim();
        while(input.length()>0){
            //this loop to clear up junk before the input.
            if(isLetter(input.charAt(0))||isNumber(input.charAt(0))){
                break; //if the first char is a letter or a number, break the loop
            }
            else input=input.substring(1);// else cut the first char off the string.
        }
        return input;
    }

    private boolean isLetter(char c)
    //returns whether or not the indicated char is an alphabetical letter.
    {
        for(int i = 0; i<letters.length; i++){
            if(letters[i]==c)return true;
        }
        return(false);
    }

    private boolean isNumber(char c)
    //returns whether or not the indicated char is a number.
    {
        for(int i = 0; i<numbers.length; i++){
            if(numbers[i]==c)return true;
        }
        return(false);
    }
}
  • 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-19T00:34:19+00:00Added an answer on June 19, 2026 at 12:34 am

    replace the method removeFirstSet with the following one

    private String[] removeFirstSet(String input)
        //removes the first set of adjecent letters from a string, and returns it.
        {
            String[] words = new String[2];
            int index = 0;
            if(input.length()<1) words[0] = "";
            while(index<input.length()){
                //this loop to retrieve the first word.
                if( isLetter(input.charAt(index))||isNumber(input.charAt(index))){
                    index++; //if the first char is a letter, move on to the next one.
                }
                else{ 
                    words[0]=input.substring(0,index);
                    words[1]=input.substring(index);
                    return words;
                }
            }
            if(index==input.length()){
                 words[0]=input.substring(0,index);
                 words[1]=input.substring(index);
                 return words;
            }
            return new String[]{"",""};
        }
    

    Hope this will solve your problem.

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

Sidebar

Related Questions

Using Goggle Analytics I'd like to use a custom variable. This is built in
i have this custom tab widget.. before i've customized it, by default i have
I have this custom textbox that I am working on and I can use
I already know the workaround for this problem, but I would like to really
This problem appears to be affecting a lot of objective-c developers working with custom
I've taken over a website that currently has a custom built CMS which we
I have a custom parser that generate C++ files and add them into a
I have this custom JSlider, which will be used in many other forms/windows. But
I have this custom validation attribute for validating a collection. I need to adapt
I have used this custom grid view in my code, and I want to

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.