-
Print problem
Ok so here is my code and the file it reads from?
can anyone tell me what the problem is here. cheers:
Code:
public void readText(String file)throws Exception{
fr = new FileReader(file);
int ch;
while(((ch = fr.read()) != -1) && ((ch = fr.read()) != ' '|| ((ch = fr.read()) != '\n'))){
System.out.print(Character.toChars(Character.toLowerCase(ch)));
// switch(ch){
// case 'a'|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'i'|'j'|'k'|'l'|'m'|'n'|'o'|'p'|'q'|'r'|'s'|'t'|'u'|'v'|'w'|'x'|'y'|'z':
// char c = (char)ch;
// token.identifier.add(Character.toString(c));
// ch = fr.read();
// break;
// case '0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9':
// char n = (char)ch;
// token.identifier.add(Character.toString(n));
// break;
// }
}
}
the test file:
{
define a = 0999;
integer integera=-12345,vara,b2b,const1, two2, three3;
a+b-c*d/e=end * ** * ;
abc+123def()
==
print(print1)}
-
Re: Print problem
How about you tell us what the problem is? What do you expect to happen? What happens instead?
-
Re: Print problem
I'm getting so t'd off I forgot to say what the problem is. HA!
when I print, it only displays a few of the ltters even though it should keep all of the characters except for the whitespaces and tabs.
I've commented out the switch case statement but there is a problem here too. I need it to store the first character is picks up if this character is a letter, it should then continue reading and if the next character is a letter or digit, it should store it along with the first character and return as an identifier.
I hope i have explained the problem well.
-
Re: Print problem
Every time you call fr.read(), it pulls another character out of the stream. That's why it is only printing every other character - it pulls one out to do the "!= -1" comparison and another one or two for the "!= ' '" and "!= '\n'" comparisons before it finally prints out to the console. You need to call fr.read() only once per each run through the loop, store it in a temporary variable, and run your checks against that.