0

so i am trying to create a private static method that will print the contents of an array with data from a text file.

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class hw2redo 
{
    public static void main(String args[])
    {
         //Scan file for data
         GeometricObject g = null;
         BufferedReader file = new BufferedReader(new FileReader("file.txt"));
            Scanner diskScanner = new Scanner(file);
            //Create dynamic array list
            List<GeometricObject> list = new ArrayList<GeometricObject>();
            //Scan data and add data to list
            while(diskScanner.hasNext())
            {
                String geolist = diskScanner.nextLine();
                g = recreateObject(geolist);

                list.add(g);

            }

            private static GeometricObject recreateObject(String data)
            {
                System.out.println(data);
            }
    }

}

However, I am getting an error my recreateObject method, saying "The method recreateObject(String) is undefined for the type hw2redo." Any help or hints as to what I'm doing incorrect would be appreciated. Thanks! Side Note: I just put something arbitrary in the method to see if the error was because it was empty. I just actually want to know how to call the private method without getting the error.

RiFF RAFF
  • 131
  • 2
  • 11

1 Answers1

1

Your recreateObject method is defined inside the main method. Move it outside the body of main:

public class hw2redo 
{
    public static void main(String args[])
    {
         //Scan file for data
         GeometricObject g = null;
         BufferedReader file = new BufferedReader(new FileReader("file.txt"));
            Scanner diskScanner = new Scanner(file);
            //Create dynamic array list
            List<GeometricObject> list = new ArrayList<GeometricObject>();
            //Scan data and add data to list
            while(diskScanner.hasNext())
            {
                String geolist = diskScanner.nextLine();
                g = recreateObject(geolist);

                list.add(g);

            }


    }

        private static GeometricObject recreateObject(String data)
        {
            System.out.println(data);
        }

}

(And, obviously, make it return something.)

James_D
  • 201,275
  • 16
  • 291
  • 322
  • I thought that private keywords could only be accessed inside the class they are created though? – RiFF RAFF Oct 11 '15 at 04:06
  • It's still inside the class. – James_D Oct 11 '15 at 04:07
  • *Facepalm*, could you explain why having it inside the main method is an issue. Sorry, I'm still a novice coder (if it wasn't blantantly obvious). – RiFF RAFF Oct 11 '15 at 04:09
  • Because you can't define a method inside a method. Methods belong to classes (or objects of the class). It simply doesn't make sense for a method to belong to another method. – James_D Oct 11 '15 at 04:09
  • Ahhh, duhhh. Thanks for pointing the obvious out to me... twice. Hopefully I'll get there. – RiFF RAFF Oct 11 '15 at 04:14