CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2018
    Location
    Delaware
    Posts
    10

    Calling Method from other Class

    Good afternoon! I am trying to call a method from other class and it wont work. Do i have it written wrong or in the wrong place?

    Code:
    public static void main(String[] args) 
                                throws FileNotFoundException, IOException     
        {  
                
            try
                {
                InputStream Filename=new FileInputStream("input.txt");
                BufferedReader br=new BufferedReader(new InputStreamReader(Filename));
    
                for (String line = br.readLine(); line != null; line = br.readLine()) 
                {
                    System.out.println(line);
                               }
               
                StringConversion.(lowerCaseString);
                
                br.close();
                }
                    catch(IOException e)
                    {
                        System.err.println("Error: Target File Cannot Be Read");
                    }
            
            
        }
          
            class StringConversion
            {
                public String lowerCaseString = "";
    
                    public String lowerCaseString(String trimmed)
                        {
                        lowerCaseString = trimmed.toLowerCase();
                        return this.lowerCaseString;
                        }
                }
    }
    any help would be appreciated. I have looked at tutorials and still cant figure out what i am doing wrong.

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Calling Method from other Class

    trying to call a method from other class
    Not sure what class and method you are talking about.
    Can you give some details?


    Here is an example from your code of calling a method that is in another class:
    Code:
               BufferedReader br=new BufferedReader(new InputStreamReader(Filename)); // define an instance
    
                for (String line = br.readLine(); line != null; line = br.readLine())   // call readLine method from this class
    it wont work.
    If there are error messages, copy the full text and paste it here.
    Norm

  3. #3
    Join Date
    Mar 2018
    Location
    Delaware
    Posts
    10

    Re: Calling Method from other Class

    I got it!!! Thank you for looking though! I was fighting it and figured out how to do it. Now on to bubble sort and selection sort! OH the joy

  4. #4
    Join Date
    Apr 2018
    Location
    North Carolina
    Posts
    1

    Re: Calling Method from other Class

    Glad you got it worked out. While it's technically fine in Java to have an instance variable with the same name as a method, I kind of like to avoid doing it. In your code above, you have lowerCaseString as a variable and a method. I guess for me having StringConversion.lowerCaseString and StringConversion.lowerCaseString() may cause some confusion, but I guess to each his own.

  5. #5
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Calling Method from other Class

    Good point.
    variable names should be nouns
    method name should be verbs
    Norm

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured