import java.util.*;
import java.io.*;
public class Animal1 {
public static void main(String [] args) throws IOException {
ArrayList<Animal> animalFile = new ArrayList<>();
try {
BufferedReader br = new BufferedReader(new FileReader("animal.txt"));
String fileRead = br.readLine();
while (fileRead != null) {
String[] tokenSize = fileRead.split(":");
String animalName = tokenSize[0];
int maxLength = Integer.parseInt(tokenSize[1]);
Animal animalObj = new Animal(animalName, maxLength);
animalFile.add(animalObj);
fileRead = br.readLine();
}
br.close();
}
catch (FileNotFoundException e){
System.out.println("file not found");
}
System.out.println("The three largest animals are: ");
}
}
これは私のコードでは、これまでのところ、それは私の動物ファイルからデータを抽出し、私は、ファイル内の各オブジェクトのために作成したアレイの出力です。 Javaで配列を比較するにはどうすれば3番目に大きい動物をプリントアウトすることができますか?私は最大長と共に改行にAnimalの名前を印刷したいと思います。比較配列 - Javaの
by whあなたは最大の動物を決めますか? –
私はコード – User786
を更新しました。** NIO **と呼ばれる新しいI/Oライブラリを 'Files'、' Paths'と 'Path'の周りで動かすことを検討してください。 'Files#lines'を使って行単位で読むことができます。 – Zabuza