ユーザーが指定したファイル名でファイルを読み込むプログラムがあります。 すべてのファイルの内容を読み取り、配列に格納する必要があります。このエラーのほかにIOを正しく行ったようです。エラーの内容を理解していますが、修正する方法がわかりません。文字列を配列に変換できません
EDIT:配列はすでにファイルに定義されています。ここで
Zoo.java:284: error: incompatible types: String cannot be converted to Animals
animals[ j ] = bufferedReader.readLine();
readFileのサブモジュールのための私のコードは次のとおりです。助けを
public String readFile(Animals[] animals)
{
Scanner sc = new Scanner(System.in);
String nameOfFile, stringLine;
FileInputStream fileStream = null;
BufferedReader bufferedReader;
InputStreamReader reader;
System.out.println("Please enter the filename to be read from.");
nameOfFile = sc.nextLine();
try
{
constructed = true;
fileStream = new FileInputStream(nameOfFile);
bufferedReader = new BufferedReader(new InputStreamReader(fileStream));
while((stringLine = bufferedReader.readLine()) != null)
{
for(int j = 0; j < animals.length; j++)
{
animals[j] = bufferedReader.readLine();
}
}
fileStream.close();
}
catch(IOException e)
{
if(fileStream != null)
{
try
{
fileStream.close();
}
catch(IOException ex2)
{
}
}
System.out.println("Error in file processing: " + e.getMessage();
}
}
感謝。
動物[]配列はどこに定義されていますか?配列を埋めるためにAnimalクラスの新しいオブジェクトを作成する必要があります。 'animal [j] =新しい動物(あなたの行); –
動物[]配列はすでに同じファイルに定義されています。 – John
すべての行を文字列として読み込むか、文字列ビルダーを使用します。次に、文字列またはstringbuilderの長さを使用して配列を作成します。最後に、forループをstring/stringbuilderとともに使用します。長さとchaAt(i) – Sedrick