Hard Light Productions Forums
Off-Topic Discussion => General Discussion => Topic started by: aldo_14 on March 26, 2002, 10:42:03 am
-
Trying to get a character / String output from java (read in from file), except I can;t get the character to output properly;
public TokenIterator ParseThroughFile() throws Exception
{
//takes the file and returns the tokens as an iterator (see implementation)
//create StreamTokeniser
Reader r = new BufferedReader(new FileReader(sourceData));
StreamTokenizer st = new StreamTokenizer(r);
st.ordinaryChar('(');
//set the following characters as ordinary, to be read as token
st.ordinaryChar(')');
st.ordinaryChar(',');
st.ordinaryChar('.');
st.ordinaryChar('\'');
st.eolIsSignificant(false);
//do not store end of lines' as tokens
st.commentChar(':');
//ignore all file lines starting with ':' when parsing
//now parse and read into iterator
tokens = new TokenIterator();
while(st.nextToken()!=st.TT_EOF)
{
tokens.add(st.toString());
}
tokens.add("EOF");
return tokens;
}
Single characters read as null (printing st.sval) or
Token[tree], line 3
Token['('], line 3
Token[tree], line 3
Token['('], line 3
Token[nil], line 3
Token[','], line 3
Token, line 3
Token[','], line 3
Token[nil], line 3
Token[')'], line 3
Token[','], line 3
Token[a], line 3
Token[','], line 3
Token[nil], line 3
Token[')'], line 3
Token['.'], line 3
with toString.
(source file contents- tree(tree(nil,b,nil),a,nil). [i/] )
I'm a bit stumped with this.... so I'm hoping someone knows a bit of java :)
-
Bah, use a LineNumberReader and a StringTokenizer. You're trying to do it the hard way... :p
-
Hmm... I can do basic Java, but this little bit of code looks a tad too complicated for me, so I'll pick up the simplest 'error' I can think of:
st.ordinaryChar(''');
That line, I assume, would not be liked too much by the Java. First it opens with a quote mark, and follows it by two more quote marks. Surely that would confuse it? Which one would it take as the end?
I'm probably way off, but I might as well try, no? :nod:
-
Yeah, that line should have an escape charachter, or use double quotes... but that would be a compile problem, not a runtime one. What exactly is it that you're trying to do anyway...?
-
Originally posted by Styxx
Yeah, that line should have an escape charachter, or use double quotes... but that would be a compile problem, not a runtime one. What exactly is it that you're trying to do anyway...?
That's a miss-type, BTW - I missed the \ in the post :)
prolog tree parser... see Question 5; http://www.cs.strath.ac.uk/teaching/ug/classes/52.222/222_P_2.html
Basically, to break a prolog tree definition ala tree(nil,a,nil).
(left, middle, right)
into tokens, and then to feed the tokens into a Binary Tree and display it.
(inc. nested trees, eg tree(tree(nil,b,nil),a,nil) )
I'm trying to split it into tokens, either strings (eg nil, node data) or single characters, like left and right brackets..... i have a StringTokenizer method written earlier, but this was a lot simpler. Plus my method removed all the punctuation - just kept letters - which I want to keep here, so I replaced it.
Edit
Stringtokenizer won't work b- or it'll be a ***** - and I can't use linereader cos it runs over mutliple lines and I don't want to do the workaround code just yet (I need to code in a line seperator later on, because of Unix string buffer restrictions). It seperates the tokens fine, it's just getting out the damn character ones that's the problem.