Java 无法将字符串转换为数组
我有一个程序,使用用户指定的文件名读取文件。 所有文件内容都必须读取并存储在数组中。除了这个错误之外,我似乎还正确地完成了IO。我知道错误是什么,但不知道如何纠正 编辑:该数组已在文件中定义 java:284:错误:不兼容的类型:无法将字符串转换为 动物 以下是我的readFile子模块代码:Java 无法将字符串转换为数组,java,arrays,io,incomplete-type,Java,Arrays,Io,Incomplete Type,我有一个程序,使用用户指定的文件名读取文件。 所有文件内容都必须读取并存储在数组中。除了这个错误之外,我似乎还正确地完成了IO。我知道错误是什么,但不知道如何纠正 编辑:该数组已在文件中定义 java:284:错误:不兼容的类型:无法将字符串转换为 动物 以下是我的readFile子模块代码: public String readFile(Animals[] animals)
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();
}
}
公共字符串读取文件(动物[]动物)
{
扫描仪sc=新的扫描仪(System.in);
字符串名称文件,字符串线;
FileInputStream fileStream=null;
BufferedReader BufferedReader;
输入流阅读器;
System.out.println(“请输入要读取的文件名”);
nameOfFile=sc.nextLine();
尝试
{
构造=真;
fileStream=新文件输入流(nameOfFile);
bufferedReader=新的bufferedReader(新的InputStreamReader(fileStream));
而((stringLine=bufferedReader.readLine())!=null)
{
对于(int j=0;j
谢谢你的帮助。
animals
是animals
的数组,但是bufferedReader.readLine()
读取行。你应该将它转换成Animal
。我看不到类animals
的定义,但是,我认为应该有一个以String
作为参数的构造函数
所以,如果我是对的,你基本上应该写:
animals[j] = new Animals(bufferedReader.readLine());
animals
是animals
的数组,但是bufferedReader.readLine()
读取行。您应该将其转换为Animal
。我看不到类animals
的定义,但是,我认为应该有一个以String
为参数的构造函数
所以,如果我是对的,你基本上应该写:
animals[j] = new Animals(bufferedReader.readLine());
代码中有很多问题。从方法的输入开始。也从文件中读取
public static void main(String[] args) {
// TODO code application logic here
for(String entry : readFile())
{
System.out.println(entry);
}
}
static public String[] readFile()
{
Scanner sc = new Scanner(System.in);
InputStreamReader reader;
System.out.println("Please enter the filename to be read from.");
String nameOfFile = sc.nextLine();
try(BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(nameOfFile))); )
{
//constructed = true; why?
String stringLine;
ArrayList<String> arraylist = new ArrayList();
while((stringLine = bufferedReader.readLine()) != null)
{
arraylist.add(stringLine);
}
return arraylist.toArray(new String[0]);
}
catch (FileNotFoundException ex)
{
Logger.getLogger(Filetoarray.class.getName()).log(Level.SEVERE, null, ex);
}
catch (IOException ex)
{
Logger.getLogger(Filetoarray.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
publicstaticvoidmain(字符串[]args){
//此处的TODO代码应用程序逻辑
for(字符串条目:readFile())
{
系统输出打印项次(输入);
}
}
静态公共字符串[]readFile()
{
扫描仪sc=新的扫描仪(System.in);
输入流阅读器;
System.out.println(“请输入要读取的文件名”);
字符串nameOfFile=sc.nextLine();
try(BufferedReader BufferedReader=new BufferedReader(new InputStreamReader(new FileInputStream(nameOfFile)));)
{
//构造=正确;为什么?
弦线;
ArrayList ArrayList=新的ArrayList();
而((stringLine=bufferedReader.readLine())!=null)
{
arraylist.add(stringLine);