2

easy question is there an other function i can use instead of println, because i want to output a non-static variable to a file usig out.println();

This is my code:

import java.io.*;

public class main {

String outputString ="Math.sqrt(25);" ;
static String outputPath ="src/output.txt";
/**
 * @param args
 */
public static void main(String[] args) throws IOException {
    File f;
    f= new File (outputPath);
        //file creation
        if(!f.exists()){
            f.createNewFile();
            System.out.println("File has been created");
        }else{
            f.delete();
            System.out.println("1. File has been deleted");
            f.createNewFile();
            System.out.println("2. File has been created");
        }
        //adding string(text) to file
        try{
            FileWriter outFile = new FileWriter(args[0]);
            PrintWriter out = new PrintWriter(outFile);

            out.println(outputString);
            out.close();

        }catch(IOException e){
                 e.printStackTrace();
            }
    }

}

if that is not posible maybe there is an whole other way to go around it. my main problem is that i want to make a string in to peace of code. But that seem to be hard to do :) any help on that :)

Neobonde
  • 85
  • 2
  • 7
  • You can certainly print both static and non-static variables with _System.out.println_. What are you trying to do that is not working? – jahroy Dec 18 '12 at 20:22
  • @jahroy.. Given that he is not doing it inside a static method. Which is what troubling OP. – Rohit Jain Dec 18 '12 at 20:23
  • 2
    Hint: when the compiler outputs an error message and you don't understand it: copy the error message, and Google it: you'll find plenty of explanations of what it means and how to fix your code. – JB Nizet Dec 18 '12 at 20:27
  • It would help if you clarified the question. It is hard to tell what is being asked. – Aaron Kurtzhals Dec 18 '12 at 20:29

3 Answers3

7

The problem has nothing to do with println(). It has to do with the fact that, being non-static, outputString is associated with an instance of your class, and your code creates no such instance.

Either make outputString static, or create an instance of main:

public void doit(String[] args) throws IOException {
    ...
    PrintWriter out = ...;
    out.println(outputString);
    ...
}

public static void main(String[] args) throws IOException {
    new main().doit(args);
}
NPE
  • 486,780
  • 108
  • 951
  • 1,012
2

The println function can print both static and non-static variables. The problem is that you're trying to access a non-static variable outputString from within a static context (your main method).

Aurand
  • 5,487
  • 1
  • 25
  • 35
1

println is a non-static method of the PrintStream class. System, also a class, has a static member called out of type PrintStream which you can retrieve via the System.out call. This member is initialized when java is started.

Note that this does not in any way imply that this is anything other than a regular Object of type PrintStream, it's just that it's a singleton that is conveniently accessible statically from System.

Joel Westberg
  • 2,656
  • 1
  • 21
  • 27