Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9009547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:13:35+00:00 2026-06-16T02:13:35+00:00

easy question is there an other function i can use instead of println, because

  • 0

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 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T02:13:36+00:00Added an answer on June 16, 2026 at 2:13 am

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As a follow up to this question: Is there an easy way to pickle
I'm sure there is an easy answer to this question, but for the life
Sorry if this is a stupid question, but is there an easy way to
This is an easy question and I want to confirm with you experts! I
I want something like a static class variable, except when different applications load my
I'm designing a declarative language for defining signal networks. I want to use variable
Is there a function that can extract two or more columns from a coeftest
I see there are other posts on this question in other languages, but I
There must be an easy answer to this question, but I'm still unable to
In an answer to a previous question: How can I use 'do I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.