import java.io.*;
import java.util.*;
import java.lang.*;

class Files
{
    public static void main(String args[])
         {
              
                 char ch;
               BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                
                 FileOutputStream f=new FileOutputStream("yo.txt",true);

                 while(ch=br.read()!='%')
                  {
                       f.write((int)ch);

                 }

              f.close();
           }

}

it shows an error at
while(ch=br.read()!='%')
saying incompatible type,though i tried typecasting..
please help

Recommended Answers

All 6 Replies

that's not the point:

I think you'll want to set your ch=br.read(); before you make the check against != '%'.
and then, add as last line in your while:
ch = br.read();

if this does not solve your problem, can you paste your entire error message here?

All you need to perform the assignment before the test is an extra set of (). ie while ( (ch=br.read()) != '%') However, you still have a problem because br.read() returns an int not a char. You can't assign an int (32 bit) to a char (16 bit), although you can cast an int to a char at your own risk. It's OK to compare an int with a char because the char is converted to int without problem, so the quickest fix to the code you posted is to replace ch with an int.

And, of course, you'll have to handle the IOException.

jamescherill i changed it to int.and i did the assignment before and testing later..but thhe data is not in the file yo.txt.its empty and my cmd is hanging.......

Maybe it's an end-of-file thing. read() returns -1 at end of file but you don't test for that, so maybe its looping at EOF with a return value -1 ?

import java.io.*;
import java.util.*;
import java.lang.*;

class Files
{
    public static void main(String args[])throws IOException
         {
              
            int ch;
          FileInputStream f=new FileInputStream("yo.txt");
            DataInputStream d=new DataInputStream(f);  
                 

                 while( (ch=d.read()) !='%')
                  {
                       f.write((int)ch);

                 }

              f.close();
           }

}

i have two errors
both are can not find symbol
1.at line 11 at file input stream
2.at line 17 at f.write()

could you please paste the entire error message? the above is saying very little.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.