Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java PrintWriter help...

  • 08-07-2004 11:38am
    #1
    Closed Accounts Posts: 111 ✭✭


    Hi Can anyone help me with a PrintWriter problem..

    I want to create an output file ti write to. I can create the file but when I try to write to it, the variable name given to the printwriter object is not recognised.

    However, it does recognise it in the main method, but not in the method I want it to recognise it in (which is in the same class, but different method)

    Bit vague Iknow, but basically, the printwriter object is only recognised in the main method of hte class, and not any other method?

    Any ideas???

    Appreciate any help,

    T.


Comments

  • Registered Users, Registered Users 2 Posts: 68,317 ✭✭✭✭seamus


    Stab in the drak without any code, but declare the PrintWriter Object outside of the main method, and all methods should be able to deal with it. ie.
    import java.io.PrintWriter;
    .....
    
    public class MyClass {
    
    private PrintWriter myWriter = null;
    private int i = 20;
    
    ......
    
    public static void main(String[] args) {
    .....
    
    myWriter = new PrintWriter(.....)
    
    ......
    }
    ......
    }
    
    Post some code if this isn't what you're looking for. :)


  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    Ya it could be a question of scope, if you declare a variable in one method you can't see it in another method.

    It could also be down to static and non-static variables.
    A static variable is a Class variable, i.e. all instances of this type of object share the same static variable.
    As opposed to an Instance variable (non-static). each instance of an object has it's own copy of this.

    You could make all your variables static and use them inside the main method (your functions need to be static too) or you could create an instance in the main method and access them like you normally would.


  • Closed Accounts Posts: 111 ✭✭TemplarBarna


    Thanks folks,...I'll give it a go when I get home...will be able to post some code then too..

    I think its something like..

    public Class MyClass {


    public static void main(String[] args) {

    PrintWriter q = new PrintWriter(new FileOutputStream("output.html"), true);

    ...........

    }

    //then later another method


    public void Method {

    q.print("testing"); //this is the bit that's not working....doesn't recognise q (I new to this)




    Thanks for the help...


  • Registered Users, Registered Users 2 Posts: 68,317 ✭✭✭✭seamus


    Yah, seems to be the problem. Simply declaring the PrintWriter outside your main method shoudl solve the issue. You'll have to make it static if you want to call it from your main method.


  • Closed Accounts Posts: 111 ✭✭TemplarBarna


    thanks a million...

    I'll try it when I get home...

    .........any chance you could help with another problem...

    I have a method in a separate class which I want to use to extract value from a Hashtable. The key of the hashtable is a name, and the value is an array ( holding anumber of scores).

    Say the hashtable is called ScoreHolder with

    key value
    ----
    Joe int[23,56,43]
    Bill int[67,87,32]
    Ann int[76,12,98]


    Basically, what I need to do is extract each name and the scores and enter them into a table (written in a html file - hence my PrintWriter problem above).

    I know it's something to do with Enumeration, but I don't fully understand how to extrat the data.

    Thanks again for the help earlier. Sounds like it will sort it out.

    T.


  • Advertisement
  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    look up looping over Hashtables and the Iterator interface.


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    Originally posted by TemplarBarna

    q.print("testing"); //this is the bit that's not working....doesn't recognise q (I new to this)


    Think of your main method as being apart from the class. So things you create in your main method cannot be seen by the class unless method is also static (afair).

    Likewise you can't access any methods in your class in the main method unless you create an object of your class first.

    So you could do something like this...
    public Class MyClass {
        private PrintWriter printWriter;
    
        public static void main(String[] args) {
            MyClass c = new MyClass(); 
    
            c.init() ;
            c.method();
        }
    
        private void init() {
            printWriter = new PrintWriter(new FileOutputStream("output.html"), true);
        }
    
        public void method {
            printWriter.print("testing"); 
        }
    
    }
    


Advertisement