簡體   English   中英

從Java中的文本文件獲取多個輸入

[英]Take multiple inputs from text files in java

我編寫了一個程序,需要為不同的輸入(整數)值生成結果。 我必須從幾個文本文件中讀取一些整數並將其存儲在這些變量中。

  • 選擇
  • numofNodes
  • numofPoints

在每個文件中,應該為上述變量之一分配多個數字,例如:

第一個文本文件包含第一個變量的五個值,其他兩個變量在單獨的行上具有一個值,如下所示:

1 2 3 4 5
60
50

第二個文本文件包含用於第二個變量的五個值,其他兩個變量在單獨的行上具有一個值,如下所示:

1 
40 50 60 70 80
50

等等..

我不知道如何從文本文件中讀取它們。 任何幫助將不勝感激。

這是我的主班:

public static void main(String[] args)throws InterruptedException {

    // how to read numbers from different text files here
    // and store them in these variables to call func method?
    // int choice = ? 
    // int numofNode = ?
    // int numofPoint = ?

    path ob=new path(choice,numofNode,numofPoint);
    ob.func();

}

將文件路徑放在字符串數組中。 那么您可以通過創建java.util.Scanner類的實例來讀取文件,該實例具有幾種讀取文件內容的方法。

您需要使用for和for-each循環遍歷文件並讀取它們。

這是一些希望對我有幫助的代碼!

/**
 * converts array of string numbers to array of integer numbers.
 *
 * @param numbers is array of strings
 * @return an integer array which is parsed from <b>numbers</b>
 *
 */
static int[] parseInt(String[] numbers) {
    return Arrays.stream(numbers).mapToInt(Integer::parseInt).toArray();
}

public static void main(String[] args) {

    // put input files path here
    String[] name_and_path_of_files = {
        "C:\\Users\\YOUR_USER\\Desktop\\input_1.txt",
        "C:\\Users\\YOUR_USER\\Desktop\\input_2.txt"
    };

    // file reader
    Scanner inputFileReader = null;
    try {

        // for all files
        for (String fileInfo : name_and_path_of_files) {

            // create reader object with file info
            inputFileReader = new Scanner(new File(fileInfo));

            int line_index = 0;

            int choices[] = null;
            int numofNodes[] = null;
            int numofPoints[] = null;

            // trying to read file content
            while (inputFileReader.hasNext()) {
                String separated_numbers[] = inputFileReader.nextLine().split(" ");
                switch (line_index) {
                    case 0:
                        choices = parseInt(separated_numbers);
                        break;
                    case 1:
                        numofNodes = parseInt(separated_numbers);
                        break;
                    case 2:
                        numofPoints = parseInt(separated_numbers);
                        break;
                }
                line_index++;
            }

            for (int choice : choices) {
                for (int numofPoint : numofPoints) {
                    for (int numofNode : numofNodes) {
                        path ob = new path(choice, numofNode, numofPoint);
                        ob.func();
                    }
                }
            }
        }
    } catch (Exception e) {
        System.out.println("Error: " + e.getMessage());
    } finally {
        if (inputFileReader != null) {
            inputFileReader.close();
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM