My task in this lab is to accept multiple input file and the format is similar in all of them except that some files have comments and I want to skip the comments lines. For example:
Input file:
Input file 1
#comment: next 5 lines are are for to be placed in an array
blah 1
blah 2
blah 3
blah 4
blah 5
#comment: next 2 line are to be placed in a different array
blah 1
blah 2
#end of input file 1
What I tried doing was I used 2 while loops (I can post my code if needed). I did the following
while(s.hasNext()) {
while(!(s.nextLine().startWith("#")) {
//for loop used to put in array
array[i] = s.nextLine();
}
}
I feel like this should work but it does not. What am I doing incorrect. Please help. Thank you in advance.
There are two problems with your code:
nextLinemore than once within the loop.whileloop will fail if there is no next line.Try modifying code as follows: