Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Can someone help please?

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can someone help please?

    Hey everybody,

    I need some help with my code. I have made a class with two methods, both with the intent to create a log file and write in it. Now, I want to call both those methods on another class file. The methods are "public static void write(String s) throws IOException" and "public static void writes(String f, String s) throws IOException". How can I call them from another class file?

  2. #2
    Junior Member
    Join Date
    Sep 2011
    Location
    philadelphia
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Can someone help please?

    You call those 2 static methods directly from another class like this

    try{
         classname.write(s);
         classname.writes(s,s);
    }catch(IOException e){
         //here is how your handle the exception
    }
    Acoolme is an Online Marketing Software Platform And Social Community

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can someone help please?

    I don't really understand the "//here is how your handle the exception"...
    These are my methods :

        public static void write(String s) throws IOException
        {
            String defaultLogFile = "log.txt";
            writes(defaultLogFile, s);
        }
     
        public static void writes(String f, String s) throws IOException
        {
            TimeZone tz = TimeZone.getTimeZone("GMT");
            Date now = new Date();
            DateFormat df = new SimpleDateFormat ("yyyy.mm.dd hh:mm:ss ");
            df.setTimeZone(tz);
            String currentTime = df.format(now);
     
            FileWriter aWriter = new FileWriter(f, true);
            aWriter.write(currentTime + " " + s + "\n");
            aWriter.flush();
            aWriter.close();
        }

    I looked up how to log in java and something like this came up...

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can someone help please?

    Also, when I tried implementing what you said, this came up : "cannot find symbol", refering to the s variable on the write method and both on the writes.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Can someone help please?

    Also, when I tried implementing what you said, this came up : "cannot find symbol", refering to the s variable on the write method and both on the writes.
    You have to pass the string variable to the methods, whatever the name of the variable is - the error is there because it is more than likely that s is not defined in the context of your code. Provide more context, and you might get a more specific answer.

    I don't really understand the "//here is how your handle the exception"...
    See Lesson: Exceptions (The Java™ Tutorials > Essential Classes)

    I looked up how to log in java and something like this came up...
    This is a very simple and rudimentary logging practice. I presume you are a novice, but will provide this here for what it is worth: for more advanced and flexible alternatives, see http://www.javaprogrammingforums.com...6-logging.html