1

I am trying to write to a .csv file, but I keep getting the error:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    void is an invalid type for the variable writeToFile
    Syntax error on token "(", ; expected
    Syntax error on token ")", ; expected

The error is associated with the line:

void writeToFile(String Filename){

Here is my code:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileWriter;

public class writeFileExample {
    public static void main(String[] args) {
        void writeToFile(String Filename){
            double steps=0;
            File file=new File(Filename);
            file.createNewFile();
            FileWriter writer=new FileWriter(file);
            try {
                //Integrate integrate=new Integrate();
                //for (steps=10;steps<1000000;steps=steps*10){
                    //double area_value=integrate.integrate_function(steps);
                    writer.write("Steps"+","+"Area");
                    //}
                //System.out.println(area_value);
                writer.flush();
                writer.close();

            System.out.println("Done");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    }
}

I can't see any syntax errors.

Taking into account Reimeus' comment below I edited it a bit. I now have:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileWriter;

void writeToFile(String Filename){
public class writeFileExample {
    public static void main(String[] args) {
            double steps=0;

etc.

I am getting the error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Filename cannot be resolved to a variable

Any help appreciated.

4
  • 1
    a big red X beside the line: It would help if you posted the actual error. It looks like you need to move createNewFile & the writer creation statement into your try/catch block Commented Feb 19, 2015 at 19:44
  • Thanks Reimeus. Do you mean within the catch statement? Commented Feb 19, 2015 at 19:45
  • 1
    1. Note to self: learn java. 2. Note to self: actually learn java. 3. Do not try to write nested methods in Java because nested methods are not supported by java. Commented Feb 19, 2015 at 21:07
  • @DwB clearly that's what I'm trying to do. Or are only those who don't need to ask questions allowed to ask questions? Commented Feb 20, 2015 at 9:06

2 Answers 2

3

Java doesnt support nested methods. Move writeToFile out of the main method

Sign up to request clarification or add additional context in comments.

Comments

0
public class Test {
    public static void main(String[] args) {
    try {
        writeToFile("c:\\abc.csv");
    } catch (IOException e) {
    // TODO Auto-generated catch block
        e.printStackTrace();
    }

    }
    static public void writeToFile(String Filename) throws IOException 
    {
      .
      .
      .
      .
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.