プログラムをコンパイルしてデバッグしましたが、出力はありません。私はBufferedReaderから配列メソッドに渡す問題を疑うが、私はそれが何であるか、またはそれを修正する方法を知っているJavaで十分ではない...助けてください! :)出力の問題:BufferedReaderから配列メソッドに渡す
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+"");
}
}
FileNotFoundExceptionはIOExceptionの一部です。両方の例外をスローする必要はなく、IOExceptionだけをスローします。 FileNotFoundExceptionがスローされた場合、「IOException:FileNotFOundException:」のようにスローされます。 – JD9999