Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用扫描仪扫描txt文件并放入对象数组_Java_Arrays_Object - Fatal编程技术网

Java 如何使用扫描仪扫描txt文件并放入对象数组

Java 如何使用扫描仪扫描txt文件并放入对象数组,java,arrays,object,Java,Arrays,Object,到目前为止,我所写的内容与我目前对数组的了解是一致的,但我不确定如何创建对象数组。我的目标是读取一个文本文件,第一个标记是数组大小,后面是课程号、部门和标题,然后将它们放入并使用扫描仪创建一个对象数组。当我编译我的代码时,它说fileScanner可能没有初始化,所以我想知道出了什么问题/我应该如何修复我的代码。任何帮助都将不胜感激 import java.util.Scanner; import java.io.*; public class Organizer{ public stati

到目前为止,我所写的内容与我目前对数组的了解是一致的,但我不确定如何创建对象数组。我的目标是读取一个文本文件,第一个标记是数组大小,后面是课程号、部门和标题,然后将它们放入并使用扫描仪创建一个对象数组。当我编译我的代码时,它说fileScanner可能没有初始化,所以我想知道出了什么问题/我应该如何修复我的代码。任何帮助都将不胜感激

import java.util.Scanner;
import java.io.*;

public class Organizer{

public static void main(String[]args){
Scanner fileScanner;
String file;
File f = null;

    do{
        try{

            System.out.print("What is the name of the input file? ");
            Scanner inputReader = new Scanner(System.in);
            file =inputReader.nextLine();
            f = new File(file);
            fileScanner = new Scanner(new File(file));

        } catch (FileNotFoundException e) {

            System.out.println("Error scanning that file, please try again.");

        }
    } while (!f.exists());


    makeArray(fileScanner);

}

public static UniCourse[] makeArray(Scanner s){

        int arraySize = s.nextInt();
        System.out.println(arraySize);
        UniCourse[] myArray = new UniCourse[arraySize];
        String title = "";
        String dept = "";
        int num;

        while(s.hasNextLine()){
            String oneLine = s.nextLine();
            Scanner lineReader = new Scanner(oneLine);
            while (lineReader.hasNext()){
                dept = lineReader.next();
                num = lineReader.nextInt();
                while (lineReader.hasNext()){
                    title = title + lineReader.next();
                }

            }
            lineReader.close();
        }
        s.close();


        return myArray;

}
}

这是我正在使用的类

public class UniCourse {

//INSTANCE VARIABLES
private String dept = "";
private int num = 0;
private String title = "";

//CONSTRUCTORS
public UniCourse(String dept, int num) {
    this.dept = dept;
    this.num = num; 
}

public UniCourse(String dept, int num, String title) {
    this.dept = dept;
    this.num = num; 
    this.title = title;
}

public UniCourse() {
    this.dept = "AAA";
    this.num = 100;
    this.title = "A course";    
}

//SETTER AND GETTER METHODS
public void setDept(String dept) {
    this.dept = dept;   
}

public void setNum(int num) {
    this.num = num;
}

public void setTitle(String title) {
    this.title = title;
}

public String getDept() {
    return this.dept;   
}

public int getNum() {
    return this.num;    
}

public String getTitle() {
    return this.title;  
}

//TOSTRING METHOD
public String toString() {
    return dept + " " + num + ": "+title;
}

}

将整个
do while
循环并
makeArray
包装在
try
块中


编译器给出错误是因为您只在try块外部声明了扫描程序,但实际上在try块内部初始化了它。我认为try块中发生的任何事情都假定在编译时不存在。

错误恰恰是编译器说,当您在代码中引入fileScanner时,您还没有初始化它

尝试:


您得到该警告是因为您的行在哪里初始化它

 fileScanner = new Scanner(new File(file));
可能无法访问,因为可能会引发
FileNotFoundException
。您捕获该异常并继续执行,以到达该行

makeArray(fileScanner);

您尝试使用它的地方。这就是警告

编译器正在抛出错误,因为您正在try块内初始化扫描仪,而try块不能保证完成,并且可能捕获异常并在任何时间点爆发

因此,您必须在try块之前初始化扫描仪,或者在try块内部添加扫描仪的用法,这样,如果在初始化扫描仪之前发现错误,就不会使用未初始化的对象


我建议将make array方法包装在try块中,正如Joel建议的那样,因为如果文件不存在,您无论如何都无法生成数组。

请添加完整的错误。
makeArray(fileScanner);