Java experts... what's wrong with this short code?

<p>public void outputFile(PrintWriter outFile, double[] simpleArray)
{
for(int x = 0; x < 8; x++)
{
outFile.printf("%1.2f", simpleArray[x]);
outFile.println();
}
outFile.close();
}</p>

<p>public static void main(String[] args) throws IOException
{
PrintWriter outFile = new PrintWriter(new File("output.txt"));
double[] simpleArray = {5, 10, 15, 20, 25, 30};
}</p>

<p>Basically, I'm just trying to create a separate method that will make a file named "output.txt" and put the values in "simpleArray" in it. But, it's not working, and I don't know what I'm doing wrong! Thanks.</p>

<p>Didn’t look at the code much but I do see that you only instantiate variables in your main method and never use your outputFile method. Maybe this?</p>

<p>I’m not exactly sure what you mean, but outputFile is a void, not static, so I don’t need to instantiate it in the main method, I believe.</p>

<p>So does outputFile have to be a non-static method?</p>

<p>I just tried out your code and it works out fine</p>

<p>import java.io.*;</p>

<p>public class DDs
{</p>

<p>public static void outputFile(PrintWriter outFile, double simpleArray)
{
for(int x = 0; x < 5; x++)
{
outFile.printf(“%1.2f”, simpleArray);
outFile.println();
}
outFile.close();
}</p>

<p>public static void main(String args) throws IOException
{
PrintWriter outFile = new PrintWriter(new File(“output.txt”));
double simpleArray = {5, 10, 15, 20, 25, 30};
outputFile(outFile, simpleArray);
}</p>

<p>}</p>

<p>just remember to change the print loop from 0 to 5</p>

<p>2 things:</p>

<ol>
<li><p>You never called the outputFile method in your main method.
Fix this with this line:
outputFile(outFile,simpleArray);</p></li>
<li><p>You used a constant in your for loop that might cause an out of bounds exception.
Fix this by changing x < 8 to x < simpleArray.length</p></li>
</ol>

<p>Wow, thanks guys! ManMan, before I was creating just a static not a static void, so it was asking me to “return” something, that’s where I was getting caught. Thanks a lot for the help.</p>

<p>@supernaut, thank you too! Your #1 requires a static method, which I’ve fixed, and your #2 was quite helpful. Thanks :)</p>