在eclipse中运行java程序时出现问题
我刚开始用java,我正在尝试做一个贪婪的算法。第一步是读取带有珠宝值和行李重量限制等的file.txt文件。不幸的是,我很难让程序运行。我正在使用eclipse,当我单击run时,我得到以下错误消息:无法启动选择,并且最近没有启动。 当我在文件树中选择java贪婪算法文件夹并选择run时,我得到以下消息选择不包含主类型。工作文件和file.txt保存在我桌面上的同一个文件夹中,但我想知道程序是否找不到它。这是我的密码:在eclipse中运行java程序时出现问题,java,eclipse,algorithm,Java,Eclipse,Algorithm,我刚开始用java,我正在尝试做一个贪婪的算法。第一步是读取带有珠宝值和行李重量限制等的file.txt文件。不幸的是,我很难让程序运行。我正在使用eclipse,当我单击run时,我得到以下错误消息:无法启动选择,并且最近没有启动。 当我在文件树中选择java贪婪算法文件夹并选择run时,我得到以下消息选择不包含主类型。工作文件和file.txt保存在我桌面上的同一个文件夹中,但我想知道程序是否找不到它。这是我的密码: /** open and read a file, and return
/** open and read a file, and return the lines in the file as a list of strings */
private List<String> readFile(file.txt)
{
List<String> records = new ArrayList<String>();
try
{
BufferedReader reader = new BufferedReader(new FileReader(file.txt));
String line;
while (( line = reader.readLine()) != null)
{
records.add(line);
}
reader.close():
return records;
}
catch (Exception e)
{
System.err.format("Exception occurred trying to read '%s'.", file.txt);
e.printStackTrace();
return null;
}
}
谢谢你的帮助。一个java类应该有一个main方法,然后只有你才能运行它 所以,你们的课将是这样的
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String... args) {
//call readFile
List<String> someList = readFile(<pass filename here>);
//do something here with someList
}
/** open and read a file, and return the lines in the file as a list of strings */
private static List<String> readFile(String filename)
{
List<String> records = new ArrayList<>();
try
{
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line;
while (( line = reader.readLine()) != null)
{
records.add(line);
}
reader.close();
return records;
}
catch (Exception e)
{
System.err.format("Exception occurred trying to read '%s'.",filename );
e.printStackTrace();
return null;
}
}
}
注意,我将readFile方法标记为static,这是因为我从main方法调用它,而没有创建测试类的实例。如果您创建了测试类的实例,并对其调用readFile方法,则可以删除静态修饰符。您必须添加一个名为void mainString[]args的方法。 这是启动程序时调用的方法。 在此主方法中,您可以调用readFile方法,如下所示:
public static void main(String[] args) {
readFile();
}
你错过了比赛
public static void main(String args[])
{
...
}
您可以在那里调用您的函数。公共静态void mainString[]args可能重复{//Code}我做了您建议的更改,但仍然收到相同的错误消息。它还说args拼写不正确。“args”是某物的占位符吗?a.Must15拼写错误必须是警告。只是想知道,您是如何在eclipse中运行代码的。当我只选择单个贪婪文件并单击run时,它表示无法启动选择,并且最近没有启动。当我选择完整的文件树并单击RunasJava应用程序时,它表示选择不包含主类型。代码中是否存在编译错误。或者,如果你使用上面的代码,它显然有一个主要的方法,我不认为有任何错误,但为了安全起见,我删除了我所有的代码并使用了你的。得到了同样的结果。