[英]Take multiple inputs from text files in java
I write a program and need to generate results for different input (integer) values. 我编写了一个程序,需要为不同的输入(整数)值生成结果。 which i have to read some integer numbers from several text files and store them in these variables. 我必须从几个文本文件中读取一些整数并将其存储在这些变量中。
in each file there might be multiple numbers that should be assigned to one of above variables, for example: 在每个文件中,应该为上述变量之一分配多个数字,例如:
First text file contains five values for first variables and other two variables have one value on separate lines like below: 第一个文本文件包含第一个变量的五个值,其他两个变量在单独的行上具有一个值,如下所示:
1 2 3 4 5
60
50
Second text file contains five values for second variables and other two variables have one value on separate lines like below: 第二个文本文件包含用于第二个变量的五个值,其他两个变量在单独的行上具有一个值,如下所示:
1
40 50 60 70 80
50
and so on.. 等等..
I have no idea how to read them from text files. 我不知道如何从文本文件中读取它们。 any helps will be appreciated. 任何帮助将不胜感激。
Here's my Main class: 这是我的主班:
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();
}
put files path in a string array. 将文件路径放在字符串数组中。 then you can read the files by creating an instance of java.util.Scanner
class that has several methods to read file content. 那么您可以通过创建java.util.Scanner
类的实例来读取文件,该实例具有几种读取文件内容的方法。
all you need is to use for and for-each loops to loop through your files and read them. 您需要使用for和for-each循环遍历文件并读取它们。
here's some code that i hope helps! 这是一些希望对我有帮助的代码!
/**
* 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.