1

so, i have to make an application that creates results, i have completed everything, I think???? but i dont know how to get me private method outputData to output the array results from the private method getValue.

This is what i have

 // CreateResults.java
// Create poll results and output them to a file.
import java.io.FileNotFoundException;
import java.util.Formatter;
import java.util.FormatterClosedException;
import java.util.IllegalFormatException;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class CreateResults
{
   private int getValue()
   {
         int[]results = { 1, 1, 1, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 8, 9 };
         outputData(results);

   } // end method getValue

   private void outputData(int[] output) 
   {

         for (int row = 0; row < results.length; row++) {
         System.out.println(input [row]);
   }//end for
   } // end method outputData

   public static void main( String args[] )
   {
      CreateResults application = new CreateResults();
      application.outputData();
   } // end main
} // end class CreateResults
Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58

2 Answers2

1

Your array exists only within method scope. Put definition outside, ,make it class variable, like this:

public class CreateResults
{
int[]results;
   private int getValue()
   {
         results = new int[] { 1, 1, 1, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 8, 9 };
         outputData(results);

   } // end method getValue
}
Alex
  • 4,457
  • 2
  • 20
  • 59
  • when i did this, the {} brackets gave me an illegal start of expression error. the line results = (int[]){ 1, 1, 1, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 8, 9 }; this line gave a lot of erros because of the brackets and i am having trouble understanding – Mathias Pettersson Apr 28 '15 at 02:53
  • Sorry. Right syntax would be: result=new int[]{1, 2, 3}; – Alex Apr 28 '15 at 03:17
1

To get your code to work, you will need to make getValue public (and as it does not return anything, make it void)

Then within your main you can then then call application.getValue() which will create your array and then call outputData

public void getValue()
{
     int[]results = { 1, 1, 1, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 8, 9 };
     outputData(results);

} // end method getValue

public static void main( String args[] )
{
  CreateResults application = new CreateResults();
  application.getValue ();
} // 

Also as you outputData is working on the inputted parameter of output you need to change it to

 private void outputData(int[] output) 
 {

     for (int row = 0; row < output.length; row++) {
         System.out.println(output[row]);
     }
 }//e
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • okay, i understand what you are saying, and did solve 2 of my errors of the three. Lastly, the line for (int row = 0; row < results.length; row++) gives an error on the variable "results" as it "cannot find symbol" and yeah i changed the input to output like right after i posted, i just forgot to change on here – Mathias Pettersson Apr 28 '15 at 02:56