所以我试图创建一个私有静态方法,用文本文件中的数据打印数组的内容。
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);
}
}
}
但是,我收到了一个错误,我的recreateObject方法,说“类型hw2redo未定义方法recreateObject(String)。”任何关于我做错的帮助或提示都将不胜感激。谢谢!旁注:我只是在方法中添加了一些任意内容,以查看错误是否因为它是空的。我实际上想知道如何在不收到错误的情况下调用私有方法。
答案 0 :(得分:1)
您的recreateObject
方法已在 main
方法中定义。将其移到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);
}
}
(显然,让它返回一些东西。)