In java, how can you read only data from a file, and ignore the strings preceding it? Please guys, I did a lot of research but I couldn't seem to find it.
在java中,如何只读取文件中的数据,并忽略它前面的字符串?伙计们,我做了很多研究,但我似乎无法找到它。
Here is a sample textfile:
这是一个示例文本文件:
number of courses:3
course numbers:219 214 114
arrival Probabilities:0.4 0.6 0.8
min time:2
max time: 4
num cups:1
simulation time:50
number of tas:2
Now as you guys can see, I just want to read the numbers.
现在,你们可以看到,我只想阅读数字。
My current code is the following, but I run into an InputMismatchException due to obvious reasons (It reads string first instead of integer):
我当前的代码如下,但由于显而易见的原因我遇到了InputMismatchException(它首先读取字符串而不是整数):
//Read file
while(input.hasNext()){
//Read Number of Courses
numCourses = input.nextInt();
courseNumbers = new int[numCourses]; //Initialize the size of courseNumbers array.
arrivalProbability = new double[numCourses]; //Initialize the size arrivalProbability array.
//Read the CourseNumbers, numCourses times.
for(int i = 0; i < numCourses; i++){
courseNumbers[i] = input.nextInt();
}
//Read the arrivalProbability, numCourses times.
for(int i = 0; i < numCourses; i++){
arrivalProbability[i] = input.nextDouble();
}
//Read minTime
minTime = input.nextInt();
//Read maxTime
maxTime = input.nextInt();
//Read number of Coffee cups
numCups = input.nextInt();
//Read simulation time
officeHrTime = input.nextInt();
//Read the number of TAs
numTAs = input.nextInt();
}
Thanks for your help in advance!
感谢您的帮助!
1 个解决方案
#1
1
I would do it like this:
我会这样做:
- read a line
- parse the line, determine which part is the label and which part is the data
- use the label to determine how to store the data
读一条线
解析该行,确定哪个部分是标签,哪个部分是数据
使用标签来确定如何存储数据
Don't use nextInt
, use nextLine
. You would need to do a lot more error checking than I have included here, but I would do it something like this:
不要使用nextInt,请使用nextLine。您需要进行比我在此处包含的更多错误检查,但我会这样做:
int numCourses;
int[] courseNumbers; // I would use a list here, but sticking with your data types for clarity.
while(input.hasNextLine()) {
String line = input.nextLine();
String[] lineParts = line.split(":");
String label = lineParts[0];
String value = lineParts[1];
if(label.equals("number of courses")) {
numCourses = Integer.parseInt(value);
} else if(label.equals("course numbers")) {
String[] courseNumberStr = value.split(" ");
courseNumbers = new int[numCourses]; // you probably want to make sure numCourses was set and courseNumStr has the correct number of elements
for(int i = 0; i < courseNumberStr.length; i++) {
courseNumbers[i] = Integer.parseInt(courseNumberStr[i]);
}
} else if( /* handle the rest of the inputs */) {
// etc
}
#1
1
I would do it like this:
我会这样做:
- read a line
- parse the line, determine which part is the label and which part is the data
- use the label to determine how to store the data
读一条线
解析该行,确定哪个部分是标签,哪个部分是数据
使用标签来确定如何存储数据
Don't use nextInt
, use nextLine
. You would need to do a lot more error checking than I have included here, but I would do it something like this:
不要使用nextInt,请使用nextLine。您需要进行比我在此处包含的更多错误检查,但我会这样做:
int numCourses;
int[] courseNumbers; // I would use a list here, but sticking with your data types for clarity.
while(input.hasNextLine()) {
String line = input.nextLine();
String[] lineParts = line.split(":");
String label = lineParts[0];
String value = lineParts[1];
if(label.equals("number of courses")) {
numCourses = Integer.parseInt(value);
} else if(label.equals("course numbers")) {
String[] courseNumberStr = value.split(" ");
courseNumbers = new int[numCourses]; // you probably want to make sure numCourses was set and courseNumStr has the correct number of elements
for(int i = 0; i < courseNumberStr.length; i++) {
courseNumbers[i] = Integer.parseInt(courseNumberStr[i]);
}
} else if( /* handle the rest of the inputs */) {
// etc
}