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.
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.)