Reputation: 13
I am a beginner programmer and CS student. I've been out of college for 6 years and have just now returned. I have a lab assignment I'm having difficulty with. Said program needs to read ints from a text file and place them in an array. Each line in the text file is a day of the week(7 lines) and the three numbers on each line are the calorie intake of breakfast, lunch, and dinner(3 numbers per line). I have all but finished the program but I'm just missing one part of the assignment.
The program must return an error and terminate if said text file does not fit the format required. The text file must be 7 lines long and each line must have 3 number(ints) seperated by spaces.
Example .txt:
100 200 450
250 300 350
275 220 500
...................
It can not look like this
100 200 450 250
300 350
275
220 500
I've considered using regex pattern recognition but regex is still well beyond me and well ahead of the class I am taking. I am sure I am making this way too difficult but here I am.
Upvotes: 1
Views: 1022
Reputation: 683
Fairly simple, we can turn this into three steps which you perform on a loop:
.split(" ")
will turn the string into an array using a space as the delimiter).length == 3
or else tell the user that their file has an error.All the while, you should be keeping track of how many lines there are. If lines != 7
then that is also a bad file.
Upvotes: 0