8 Replies Latest reply: Apr 6, 2007 10:45 AM by 807599 RSS

    return a string

    807599
      I defined a string in a class, then I am going to call it in the main class.
      But how?
      public class FileDownload {
          private String f;
          
           public  void download(String address, String localFileName) {
                OutputStream out = null;
                URLConnection conn = null;
                InputStream  in = null;
                      int j = localFileName.lastIndexOf('=');
                      f = localFileName.substring(j+1);
      ...
      here f is a string.
      import java.io.*;
      
      public class Parse 
      {
              public static void main(String args[])
           {
                      // args.length is equivalent to argc in C
                  FileDownload d = new FileDownload();
                  d.download(args[0]); 
                 // String f = d.getFileName();
                      if (args.length == 2)
                {
                              try
                     {
                                     
                          BufferedReader br = new 
                                              BufferedReader(new FileReader(d.f));//I am going to put f here
      ...
        • 1. Re: return a string
          807599
          The correct way to do what you want to do is to create a methond in the FileDownload class that returns your string f called getF (or something like that). The other option is to declare String f as public and the main class could should be able to access it by d.f, but this is frowned upon normally.
          • 2. Re: return a string
            807599
            Hi,

            Change the access specifier from private to protected in your FileDownload class.

            or change your method signature from
             public  void download(String address, String localFileName) 
            to
             public  String download(String address, String localFileName) 
            and return a string
            Your download method accepts 2 parameters but you have passed only one parameter :)
            Check your code

            bye for now
            sat
            • 3. Re: return a string
              807599
              I have created a method. bur it is not working
              In the first class
              public String getFileName()
                      {
                          return f;
                      }
              Second class
              public class Parse 
              {
                      public static void main(String args[])
                   {
                              // args.length is equivalent to argc in C
                          FileDownload d = new FileDownload();
                          d.download(args[0]); 
                         String f = d.getFileName();
                              if (args.length == 2)
                        {
                                      try
                             {
                                             
                                  BufferedReader br = new 
                                                      BufferedReader(new FileReader(f));
              Can't find the method getFileName.????
              • 4. Re: return a string
                807599
                As AnanSmriti mentioned, you are going to need to pass the download method two arguments to get that part to work correctly. But besides that, the method looks good and you are calling it right. The only thing I can think of is that the FileDownload class hasn't been recompiled yet. If it has been recompiled, you might want to post more of the code for debugging.
                • 5. Re: return a string
                  807599
                  javaftw,
                  thanks,
                  method download is overloaded.
                  please see more code
                  [
                  public  void download(String SearchTitle) {
                           String address = "http://www.xxxxx.com";     
                              int lastSlashIndex = address.lastIndexOf('/');
                            if (lastSlashIndex >= 0 &&
                                lastSlashIndex < address.length() - 1) {
                                 download(address, address.substring(lastSlashIndex + 1));
                  • 6. Re: return a string
                    807599
                    init:
                    deps-jar:
                    Compiling 2 source files to C:\Documents and Settings\Owner\Parse\build\classes
                    C:\Documents and Settings\Owner\Parse\src\Parse.java:12: cannot find symbol
                    symbol : method getFileName()
                    location: class FileDownload
                    String f = d.getFileName();
                    1 error
                    BUILD FAILED (total time: 3 seconds)
                    when I use eclipse, more errors comes before compiling.
                    They are not the compiling errors, just red cross alerts marked.

                    Below warnings come from eclipse
                    The method download(String) is undefined for the type FileDownload.
                    The method getFileName is undefined for the type FileDownload.
                    • 7. Re: return a string
                      807599
                      Are they in same package?

                      bye for now
                      sat
                      • 8. Re: return a string
                        807599
                        Post the full Code, including getFileName().