954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Files

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

mallikaalokam
Newbie Poster
23 posts since Feb 2012
Reputation Points: 10
Solved Threads: 1
 

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?

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

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.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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.......

mallikaalokam
Newbie Poster
23 posts since Feb 2012
Reputation Points: 10
Solved Threads: 1
 

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 ?

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
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()

mallikaalokam
Newbie Poster
23 posts since Feb 2012
Reputation Points: 10
Solved Threads: 1
 

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

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: