在 Java 中返回一个 int 数组

Returning an int array in Java

所以我需要一个 return 文本文档数组的脚本。该文件是所有数字。每次我尝试 运行 它时,我都会收到此消息“此方法必须 return 类型为 int[] 的结果”,这是在检查器部分下。我正在尝试 return 到另一个 class 以便我可以将数组用于 if 语句。谁能帮我解决这个问题?

import java.io.File; // Import the File class
import java.io.FileNotFoundException; // Import this class to handle errors
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Scanner; // Import the Scanner class to read text files

public class ReadFile {
public static int[] checker(){
    try {
        File myObj = new File("Tracker.txt");
        Scanner myReader = new Scanner(myObj);
        while (myReader.hasNextLine()) {
            String data = myReader.nextLine();
            String[] moreData = data.split("\n");
            int day = Integer.parseInt(moreData[0]);
            int Posts = Integer.parseInt(moreData[1]);
            return new int[] {day, Posts};
        }
        
        myReader.close();
    } catch (FileNotFoundException e) {
        return new int[] {0, 0};
    }
    }

}

预期结果是:{0, 40}

编译器报错是因为它认为有一条可能的退出路径会跳过所有其他 return 语句。如果第一次互动 hasNextLine returns false 会怎样?

在这种情况下,您应该将 catch 子句中的 return 移动到方法的末尾

import java.io.File; // Import the File class
import java.io.FileNotFoundException; // Import this class to handle errors
import java.util.Scanner; // Import the Scanner class to read text files

public class ReadFile {

    public static int[] checker() {
        File myObj = new File("Tracker.txt");
        // This will ensure that the Scanner is closed when
        // you exit from the loop early
        try (Scanner myReader = new Scanner(myObj)) {
            while (myReader.hasNextLine()) {
                String data = myReader.nextLine();
                // Why?  You just read the line, there
                // are no more lines in this text?!
                String[] moreData = data.split("\n");
                int day = Integer.parseInt(moreData[0]);
                int Posts = Integer.parseInt(moreData[1]);
                // Why?? Are there only two values in the
                // whole file?!  Then you should use if (...) instead of while (...)
                return new int[]{day, Posts};
            }
        } catch (FileNotFoundException e) {
            // You should at least print an error   
        }
        return new int[]{0, 0};
    }

}

查看 The try-with-resources Statement 了解有关 try (Scanner myReader = new Scanner(myObj)) { 正在做什么的更多信息

They numbers seperated by line breaks like so : 0 40 30

所以,更好的解决方案可能类似于...

public static int[] checker() {
    File myObj = new File("Tracker.txt");
    List<Integer> values = new ArrayList<Integer>(128);
    try (Scanner myReader = new Scanner(myObj)) {
        while (myReader.hasNextLine()) {
            String data = myReader.nextLine();
            int value = Integer.parseInt(data);
            values.add(value);
        }
    } catch (FileNotFoundException e) {
        System.out.println("Something went wrong");
    }
    return values.stream().mapToInt(Integer::intValue).toArray();
}

使用这个数组的问题在于,您不知道有多少行(或元素),因此,您需要一些更动态的东西,比如 List,它你可以不断地添加新元素,而不需要知道你需要预先存储多少。

这有点“高级”,但一般来说,在大多数情况下我会考虑在数组上使用 List,但我就是这样,而且我很懒。

Expected outcome is: {0, 40}

如果您只想要固定数量的元素,那么请计算您已阅读的行数并在到达时尽早退出

public static int[] checker() {
    File myObj = new File("Tracker.txt");
    try (Scanner myReader = new Scanner(myObj)) {
        int line = 0;
        int[] values = new int[2];
        while (myReader.hasNextLine()) {
            String data = myReader.nextLine();
            int value = Integer.parseInt(data);
            values[line] = value;
            line += 1;
            if (line > 1) {
                // Early exit
                break;
            }
        }
        return values;
    } catch (FileNotFoundException e) {
        System.out.println("Something went wrong");
    }
    return new int[] { 0, 0 };
}