我已经编译并调试了我的程序,但是没有输出。我怀疑bufferedreader传递给array方法的问题,但我对java不够精通,不知道它是什么,也不知道如何修复它。。。请帮忙!:)
public class Viennaproj {
private String[] names;
private int longth;
//private String [] output;
public Viennaproj(int length, String line) throws IOException
{
this.longth = length;
this.names = new String[length];
String file = "names.txt";
processFile("names.txt",5);
sortNames();
}
public void processFile (String file, int x) throws IOException, FileNotFoundException{
BufferedReader reader = null;
try {
//File file = new File("names.txt");
reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void sortNames()
{
int counter = 0;
int[] lengths = new int[longth];
for( String name : names)
{
lengths[counter] = name.length();
counter++;
}
for (int k = 0; k<longth; k++)
{
int counter2 = k+1;
while (lengths[counter2]<lengths[k]){
String temp2;
int temp;
temp = lengths[counter2];
temp2 = names[counter2];
lengths[counter2] = lengths[k];
names[counter2] = names[k];
lengths[k] = temp;
names[k] = temp2;
counter2++;
}
}
}
public String toString()
{
String output = new String();
for(String name: names)
{
output = name + "/n" + output;
}
return output;
}
public static void main(String[] args)
{
String output = new String ();
output= output.toString();
System.out.println(output+"");
}
}
1条答案
按热度按时间w8rqjzmb1#
在java中,publicstaticvoidmain(string[]args)方法是应用程序的起点。
您应该在main方法中创建viennaproj的对象。看看你的实现,仅仅创建一个viennaproj对象就能修复你的代码。
您的主要方法如下所示
而且,如果在执行此操作时遇到filenotfound异常,则意味着java无法找到该文件。
必须提供文件的完整文件路径才能避免此问题(例如:“c:/test/input.txt”)