aspose file tools*
The moose likes Java in General and the fly likes urgent please help...look at code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "urgent please help...look at code" Watch "urgent please help...look at code" New topic
Author

urgent please help...look at code

pradeep arum
Ranch Hand

Joined: Oct 01, 2003
Posts: 130
Hi all,
I am trying to build a lexical analyser here,but when i try to do the below code ,i think i am unable to find an END OF FILE character ,please help
thanks
//i had only the text yy in the in.txt file
//it goes into infinite loop,is there a problem with seek()?
public class MARK {
public static void main(String[] args) throws IOException {
RandomAccessFile in = new RandomAccessFile("in.txt","r");
int c,i=0,flag_T=0,j=0;
char cc;
long pos=0;
while ((c = in.read()) != -1){
System.out.println("ch="+(char)c);
char ch=(char)c;
while (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9'){
pos=in.getFilePointer();
c=in.read();
}
in.seek(pos);
System.out.println("2");
}//while
}//main
}//MARK


SCJP1.4,SCBCD
Failure is not when you fall down; its only when you fail to get up again.
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
I tried this code, it terminates OK with output
ch=y
2
what is the infinite loop you mentioned ?

Originally posted by pradeep arum:
Hi all,
I am trying to build a lexical analyser here,but when i try to do the below code ,i think i am unable to find an END OF FILE character ,please help
thanks
//i had only the text yy in the in.txt file
//it goes into infinite loop,is there a problem with seek()?
public class MARK {
public static void main(String[] args) throws IOException {
RandomAccessFile in = new RandomAccessFile("in.txt","r");
int c,i=0,flag_T=0,j=0;
char cc;
long pos=0;
while ((c = in.read()) != -1){
System.out.println("ch="+(char)c);
char ch=(char)c;
while (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9'){
pos=in.getFilePointer();
c=in.read();
}
in.seek(pos);
System.out.println("2");
}//while
}//main
}//MARK


not so smart guy still curious to learn new stuff every now and then
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Just a logical error. If your file contains "yy", this program will halt. But it contains, for example, "yy\n", you'll get an infinite loop. Imagine that you've read both y's. In the inner while loop, you read the "\n" and break out of the loop. "pos" has been set to immediately before the newline. Now, you start the outer loop again. The condition of the outer loop reads the "\n" character again. Because it's not a character or digit, the inner while loop is not entered. Therefore, we fall through to the seek() call, resetting the file so that the "\n" will be read again, next. There's your infinite loop!


[Jess in Action][AskingGoodQuestions]
pradeep arum
Ranch Hand

Joined: Oct 01, 2003
Posts: 130
thanks ernest,
this is going into an infinite loop ,in.txt contains abcde ,please help,anybody.
pradeep

import java.io.*;
public class MARK {
public static void main(String[] args) throws IOException {
RandomAccessFile in = new RandomAccessFile("in.txt","r");
int c,i=0,flag_T=0,j=0;
char cc;
long pos=0;
while ((c = in.read()) != -1){
System.out.println("ch4="+(char)c);
char ch=(char)c;
while ((c >= 'a' && c <= 'z' )|| (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')){
c=in.read();
System.out.println("ch="+(char)c);
}
pos=in.getFilePointer();
in.seek(pos-1);//here i am trying to go one character back to get to
// the EOF character
System.out.println("2");
}//while
}//main
}//MARK
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

Please learn how to use *code* tags.
Why are you trying to use two loops to do this? That confuses me. Which loop are you stuck in? Adding print statements will help determine that. Knowing that may help you solve the problem.
Look up 'System.getProperty("line.separator")' for something like this (notice the nice *code* tags):


Please ignore post, I have no idea what I am talking about.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

i think i am unable to find an END OF FILE character

I think I side-tracked myself. You are asking about "end of file". Your outer loop handles that correctly.

This will leave when it sees that end of file.
The problem is that you have another loop inside of this (I think).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: urgent please help...look at code