8

I am working in school project. In that what they told is, I will be given a String which contains an actual program like....

import java.io.*\npublic class A{\n...........EOF

And My job is to find particular regular expressions in that String(Program).

Now My question is..

void myFunc(String s)
{
 while(s.charAt(i) != EOF) /*needed replacement for EOF*/
 {
 // Actual Code
 }
}

In the above code, how to find whether EOF is reached in a string?

Muthu Ganapathy Nathan
  • 3,199
  • 16
  • 47
  • 77

3 Answers3

18

It's unlikely that you need this - you probably just need to read till the end of the string, and since it's a representation of a file's contents, your teacher referred to it as EOF.

However...

There is a character called EOF. It's also called control-Z, because that's how you type it. If you want to include it in a string, you have to type it as "\u001a" as in:

String iHaveAnEof = "file ends here\u001a";

If you really need this, your teacher is probably older than me, and I'm probably old enough to be your grandfather ;-)

Ed Staub
  • 15,480
  • 3
  • 61
  • 91
  • I don't know for which system you are talking about, but on GNU, it is Ctrl-D. – AnonBird Nov 26 '16 at 18:41
  • @AnonBird Most DEC systems, CP/M, MS-DOS and Windows used control-Z. See "EOF", [ASCII Wikipedia page](https://en.wikipedia.org/wiki/ASCII). – Ed Staub Nov 27 '16 at 00:45
  • I wasn't doubting about your answer, sorry if it looked like it, but just an update to your answer for gnu users. – AnonBird Nov 27 '16 at 08:47
4

There is no EOF character in a string. You just need to iterate over the characters in the string:

for (int i = 0; i < s.length(); i++){
    char c = s.charAt(i);        
    //Process char
}
Community
  • 1
  • 1
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • Ya. I too got confused in that only. Then How could I do that in this case? – Muthu Ganapathy Nathan Sep 28 '11 at 23:33
  • Ha! I thinked a lot. But not this. well I try for this one. – Muthu Ganapathy Nathan Sep 28 '11 at 23:36
  • there might be some eof character in there: http://en.wikipedia.org/wiki/End-of-file – Ray Tayek Sep 29 '11 at 00:37
  • @Ray Tayek Why exactly would java worry about implementation details of the C standard library? The C standard doesn't even specify which value EOF has to have.. – Voo Sep 29 '11 at 01:06
  • @Voo java would not. but you might have to read a file with a control z in it. – Ray Tayek Oct 06 '11 at 00:12
  • @RayTayek Terminal EOFs are an implementation detail, some terminals don't even have one and in either case Java luckily completely abstracts that concept away. If one reads a file with whatever representation ctrl-z has, it's just that sign and doesn't have any specific meaning (eg I can trivially create a file containing that character and that won't limit me in any way). – Voo Oct 06 '11 at 12:38
-1

BufferedReader rd=new BufferedReader(new InputStreamReader(new FileInputStream("Input.txt"),"UTF-8"));

  char ch;
  int r;
  while(true)
  {
    r=rd.read();    //Reading a character in integer format
    if(r==-1)   //Checking for the End of File
      break;
    else
    {
      ch=(char)r;   //Converting to char format
  System.out.println(ch);
    }
  }