I donโt know why I struggle with this so badly, but any help would be much appreciated.
I create my own tokenizer that accepts a file with a list of commands, delimiters and values. Then it displays each "token" along with what type it is.
INPUT: AND 3, 4, 5 ; some comments AND 3, 4, 5 ; some comments
I need to output:
AND --- command 3 --- value , --- delimiter 4 --- value , --- delimiter 5 --- value
I am working right now, where I deduced:
AND 3, 4, 5 --- delimiter
but I need to break it further.
This is where I am now:
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 the real limitations that I have with this assignment cannot use StringTokenizer and regex.
source share