?
Solved

BufferedReader: cannot resolve symbol

Posted on 2003-02-21
10
Medium Priority
?
704 Views
Last Modified: 2012-08-14
Hi,
I'm a begginer with Java and I have a problem when I try to compile a test class. I've been looking for a solution reading a lot of messages sent to forums, but I don't identify the solution. The problem is:

-------------------------------- The code:

import java.io.*;
public class prueba {
   
    public void main() {
        int i;
        i = fileCopy();
    }
   
    public int fileCopy() {
        BufferedReader entrada = new BufferedReader(new FileReader("texto.txt"));
        PrintWriter salida = new PrintWriter(new FileWriter("tocoto.txt"), true);
        String line;
        while((line = entrada.readLine()) != null) {
            salida.println(line);
        }
        entrada.close();
        salida.close();
        return 1;
    }
} // -- end class prueba

--------------------------- The compilation output:
Command line:
javac -classpath ".;C:\Documents and Settings\rafa\Mis documentos\MisWebs\Java\prueba1" -g -verbose "C:\Documents and Settings\rafa\Mis documentos\MisWebs\Java\prueba1\prueba.java"

[parsing started C:\Documents and Settings\rafa\Mis documentos\MisWebs\Java\prueba1\prueba.java]
[parsing completed 110ms]
[checking prueba]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/lang/Object.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/io/BufferedReader.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/io/FileReader.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/io/FileDescriptor.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/lang/String.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/io/File.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/io/InputStreamReader.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/io/InputStream.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/io/Reader.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/io/PrintWriter.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/io/FileWriter.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/io/OutputStreamWriter.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/io/OutputStream.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/io/Writer.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/lang/Error.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/io/FileNotFoundException.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/io/IOException.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/lang/Exception.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/lang/Throwable.class)]
[loading C:\Archivos de programa\Java\j2re14101\lib\rt.jar(java/lang/RuntimeException.class)]
C:\Documents and Settings\rafa\Mis documentos\MisWebs\Java\prueba1\prueba.java:11: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
        BufferedReader entrada = new BufferedReader(new FileReader("texto.txt"));
                                                    ^
C:\Documents and Settings\rafa\Mis documentos\MisWebs\Java\prueba1\prueba.java:12: unreported exception java.io.IOException; must be caught or declared to be thrown
        PrintWriter salida = new PrintWriter(new FileWriter("tocoto.txt"), true);
                                             ^
C:\Documents and Settings\rafa\Mis documentos\MisWebs\Java\prueba1\prueba.java:14: unreported exception java.io.IOException; must be caught or declared to be thrown
        while((line = entrada.readLine()) != null) {
                             ^
C:\Documents and Settings\rafa\Mis documentos\MisWebs\Java\prueba1\prueba.java:17: unreported exception java.io.IOException; must be caught or declared to be thrown
        entrada.close();
               ^
[total 3154ms]
4 errors

--------------------------- The CLASSPATH value:

.;C:\Archivos de programa\Java\j2re14101\lib

--------------------------- The LIBPATH value:

.;C:\Archivos de programa\Java\j2re14101\lib

-------------------------------------------------------
Thanks in advance,
0
Comment
Question by:rcastan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 7992759
Your copying method must catch the IOException thrown by various parts of the code. You main methos is also not defined properly:


   public static void main(String args[]) {
       int = fileCopy();
   }
   
   public int fileCopy() {
     try {
       BufferedReader entrada = new BufferedReader(new FileReader("texto.txt"));
       PrintWriter salida = new PrintWriter(new FileWriter("tocoto.txt"), true);
       String line;
       while((line = entrada.readLine()) != null) {
           salida.println(line);
       }
       entrada.close();
       salida.close();
       return 1;
}
catch (IOException e) {
  return -1;
}
   }
} // -- end class prueba
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7992764
>>int = fileCopy();

should be

int i = fileCopy();

sorry
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 7993863
Try this:-

public static void main(String args[]) {
      int = fileCopy();
}
 
  public int fileCopy() {
    BufferedReader entrada = null;
    PrintWriter salida = null;
    try {
      entrada = new BufferedReader(new FileReader("texto.txt"));
      salida = new PrintWriter(new FileWriter("tocoto.txt"), true);
      String line;
      while((line = entrada.readLine()) != null) {
          salida.println(line);
      }
      return 1;
   }
   catch (IOException e) {
      return -1;
   }finally {
       try {
       if(entrada!=null)
          entrada.close();
       if(salida!=null)
          salida.close();
       }catch(Exception ex) {
              ex.printStackTrace();
       }
   }
  }
} // -- end class prueba
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:rcastan
ID: 7998356
-----------------------------CEHJ:
It just worked fine. Only two comments:
- I had to declare the fileCopy method as "static"
(non-static method fileCopy() cannot be referenced from a static context-main()-)
- the i variable is defined as int in the code I sent:
public void main() {
  int i;
  i = fileCopy();
}

----------------------------Ajay-Singh:
The problem persists in the code you sent me:

----compilation results:
prueba2.java [18:1] cannot resolve symbol
symbol  : class BufferedReader
location: class prueba2
   BufferedReader entrada = null;
   ^
prueba2.java [19:1] cannot resolve symbol
symbol  : class PrintWriter
location: class prueba2
   PrintWriter salida = null;
   ^
prueba2.java [21:1] cannot resolve symbol
symbol  : class BufferedReader
location: class prueba2
     entrada = new BufferedReader(new FileReader("texto.txt"));
                   ^
prueba2.java [21:1] cannot resolve symbol
symbol  : class FileReader
location: class prueba2
     entrada = new BufferedReader(new FileReader("texto.txt"));
                                      ^
prueba2.java [22:1] cannot resolve symbol
symbol  : class PrintWriter
location: class prueba2
     salida = new PrintWriter(new FileWriter("tocoto.txt"), true);
                  ^
prueba2.java [22:1] cannot resolve symbol
symbol  : class FileWriter
location: class prueba2
     salida = new PrintWriter(new FileWriter("tocoto.txt"), true);
                                  ^
prueba2.java [29:1] cannot resolve symbol
symbol  : class IOException
location: class prueba2
  catch (IOException e) {
         ^
7 errors
------------End compilation results.
Is it maybe due to declaring the variables "entrada" and "salida" outside de "try" container?

Thank you both a lot for your time (and knowledge!)
Regards...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8000591
>>I had to declare the fileCopy method as "static"

That's true if you want to call it without first creating an instance of prueba, yes.

>>the i variable is defined as int in the code I sent:

The code i posted is functionally identical to yours, just shorter to write.
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 8002600
Adding

import java.io.*;

Should solve all these compilation problems.
0
 

Author Comment

by:rcastan
ID: 8003591
Again, thanks a lot for your comments.
The sample was compilated succesfully and works. I'm reading now the book "JAVA 2: Fundamentals (Horstmann & Cornell)" trying to improve my skills programming in Java.
Anyway, I've recovered my inner-peace with my "super-application ;-) " compiled and running ... and this is thanks to you.

NOTE.-
Ajay-Singh: In the first post I sent the code and the compilation results. As you can see, it included the "import java.io.*;" statement and in the compilation results the needed classes appear to be loaded.. I don't understand your recommendation.

Regards,
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8003621
At *some* point, the compiler could not find BufferedReader, etc. That's why Ajay-Singh recommended importing io.

A points split would be nice then rcastan!
0
 

Author Comment

by:rcastan
ID: 8003997
Please, can you tell me what shall I do at this point to close properly the question?
Thanks in advance.
Regards
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 150 total points
ID: 8004011
Sure - ask CS (http://www.experts-exchange.com/Community_Support/) to do a points split for you.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Suggested Courses
Course of the Month11 days, 13 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question