debugging - Parser missing last word - Java -



debugging - Parser missing last word - Java -

this custom built parser, because dislike way default java.util.scanner works.

my problem when create parser using new parser("parsed phrase here") or function reloadbuffer("parsed phrase here") misses lastly word of input. tried create code readable could, it's still pretty dense, sorry that. oh, , if ever gets fixed, sense free utilize 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 empty parser. */ public parser() { words = new arraylist<string>(); buffer = ""; wordindex = 0; } /** * assembles parser given string input. uses reloadbuffer(); * @see reloadbuffer */ public parser(string input) { words = new arraylist<string>(); reloadbuffer(input); } /** * parses each word/set of chars array. must called before retreiving of words can done. * core function of class. careful if edit part. */ public void parsebuffer(){ string input = buffer; while(input.length()>=1) { input = trimtoselectedchars(input); words.add(removefirstset(input)[0]); input = removefirstset(input)[1]; } } /** * resets array given string input. used in primary constructor. uses parsebuffer(); * @see parsebuffer() */ public void reloadbuffer(string input){ buffer = input; wordindex = 0; parsebuffer(); } /** * @return next word parsed string, based upon value of wordindex. */ public string next(){ wordindex++; if (wordindex<= words.size()+1){ seek {return words.get(wordindex-1); } catch(exception ex) { system.err.println("error: reached end of list. resetting index 0."); resetindex(); } } homecoming ""; } //notice when using wordat(), leaves index selected, , not revert before. /** * @return word @ indicated index, much charat() function, using next() function. sets wordindex input index. * @see string * @see next() */ public string wordat(int index){ wordindex = index; homecoming next(); } /** * @return first word parsed input. * @see string */ public string firstword() { homecoming wordat(0); } /** *be careful in using lastword() sets wordindex lastly value homecoming string *@return lastly parsed word input. */ public string lastword(){ homecoming wordat(words.size()-1); } /** * resets wordindex 0, beginning. */ public void resetindex(){wordindex = 0;} /** * homecoming whether or not there word in parser list. */ public boolean hasnext(){ homecoming (wordindex<words.size()); } //internal methods here. private string[] removefirstset(string input) //removes first set of adjecent letters string, , returns it. { string[] words = new string[2]; int index = 0; if(input.length()<1) words[0] = ""; while(index<input.length()){ //this loop retrieve first word. if(isletter(input.charat(index))||isnumber(input.charat(index))){ index++; //if first char letter, move on next one. } else{ words[0]=input.substring(0,index); words[1]=input.substring(index); homecoming words; } } homecoming new string[]{"",""}; } private string trimtoselectedchars(string input) //trims not letter front end of string. { input = input.trim(); while(input.length()>0){ //this loop clear junk before input. if(isletter(input.charat(0))||isnumber(input.charat(0))){ break; //if first char letter or number, break loop } else input=input.substring(1);// else cutting first char off string. } homecoming input; } private boolean isletter(char c) //returns whether or not indicated char alphabetical letter. { for(int = 0; i<letters.length; i++){ if(letters[i]==c)return true; } return(false); } private boolean isnumber(char c) //returns whether or not indicated char number. { for(int = 0; i<numbers.length; i++){ if(numbers[i]==c)return true; } return(false); } }

replace method removefirstset next one

private string[] removefirstset(string input) //removes first set of adjecent letters string, , returns it. { string[] words = new string[2]; int index = 0; if(input.length()<1) words[0] = ""; while(index<input.length()){ //this loop retrieve first word. if( isletter(input.charat(index))||isnumber(input.charat(index))){ index++; //if first char letter, move on next one. } else{ words[0]=input.substring(0,index); words[1]=input.substring(index); homecoming words; } } if(index==input.length()){ words[0]=input.substring(0,index); words[1]=input.substring(index); homecoming words; } homecoming new string[]{"",""}; }

hope solve problem.

java debugging parsing

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -