Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.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

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 如何修复我的读取文件方法?_Java_Arrays_String_For Loop_Try Catch - Fatal编程技术网

Java 如何修复我的读取文件方法?

Java 如何修复我的读取文件方法?,java,arrays,string,for-loop,try-catch,Java,Arrays,String,For Loop,Try Catch,读取文件: 此方法只有一个参数,即文件名。 此方法的目的是将文件内容读入数组。 您必须使用try/catch块和scanner对象。 文件的第一行有一个整数,用于指定行数。 将此值读入变量numberline。 再次呼叫nextLine以放弃该行的其余部分 将fileContents数组分配给numberLineThe元素。 编写一个for循环,将指定数量的行读取到fileContents中。 catch异常代码不应执行任何操作或报告错误 这是我当前的代码。我做错了什么 public void

读取文件:

此方法只有一个参数,即文件名。
此方法的目的是将文件内容读入数组。
您必须使用try/catch块和scanner对象。
文件的第一行有一个整数,用于指定行数。
将此值读入变量numberline。
再次呼叫nextLine以放弃该行的其余部分
将fileContents数组分配给numberLineThe元素。
编写一个for循环,将指定数量的行读取到fileContents中。
catch异常代码不应执行任何操作或报告错误

这是我当前的代码。我做错了什么

 public void readFile(String filename) {
        // TODO Auto-generated method stub
        try {

            Scanner read = new Scanner(new File(filename));
            int[] fileContents = {numberLines};
            numberLines = read.nextInt();
            read.nextLine();

            for(int i = 0; i < numberLines; i ++){

                fileContents[i] = read.nextInt();

                read.close();
            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
public void readFile(字符串文件名){
//TODO自动生成的方法存根
试一试{
扫描仪读取=新扫描仪(新文件(文件名));
int[]fileContents={numberLines};
numberLines=read.nextInt();
read.nextLine();
for(int i=0;i
公共无效读取文件(字符串文件名){
//TODO自动生成的方法存根
试一试{
扫描仪读取=新扫描仪(新文件(文件名));
//int[]fileContents={numberLines};//fileContents有一个元素
numberLines=read.nextInt();
//您可以先读取numberLines,然后根据需要创建fileContents数组
int[]fileContents=新的int[numberline];
//若每一行都只包含一个数字,那个么可以将行读入字符串并将其转换为int
//read.nextLine();
for(int i=0;i
可能会引发一些异常:

NoTouchElementException
——如果找不到行


IllegalStateException
——如果此扫描仪已关闭

,则不会显示。当我提交赋值时,那部分只是说,“不能调用方法:readFile”
public void readFile(String filename) {
        // TODO Auto-generated method stub
        try {

            Scanner read = new Scanner(new File(filename));
            //int[] fileContents = {numberLines}; //fileContents has one element
            numberLines = read.nextInt();
            //you can read numberLines first and then create fileContents array as you wish
            int[] fileContents = new int[numberLines];
            //if each line only contains a number, you can read line to String and convert it to int
            //read.nextLine();

            for(int i = 0; i < numberLines; i ++){
                String nextLine = read.nextLine();
                //fileContents[i] = read.nextInt();
                fileContents[i] = Integer.parseInt(nextLine);
                //read.close();
            }
            //close read after all lines were read
            read.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }