the -999 at end of each line is my sentinel.
Output: sum of each line.
I can get it to read and output sum of 1st line but cant onto second line.
This is my 1st time posting a code qu up so if anyone helps me out, be gentle!
Here be my code
My output is:
Line 1: Total = 420
Line 2: Total = 420
Line 3: Total = 420
Line 4: Total = 420
The 420 value is the first lines total
The book im studying with has this as an example but doesnt give the proper code to properly try test it.
p.s. i am a total novice!!!
I put 'code tags' around your java. that makes it much easier to read by preserving your indentation and color-highlighting various keywords.
Notice that line 10 is inside your for-loop. That means that each time you come around, you create a new Scanner/FileReader, which re-opens the file and re-points it back to the first line.
What you need to do is use the FileReader to loop through each line. THEN use your Scanner to parse each line as you go.
As a general note, hard-coding your for loop on the number of the lines in the file is a bad idea. What if you don't know how many lines the file will have? A better idea is to create your file reader outside the loop, then have a while-loop that continues until your FileReader returns 'no more lines'. if you search around, I'm sure you can find an example.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
If you are using a Scanner on the input, why don't you use the Scanner to read the files? You can use the hasNextLine() method for the continuation condition for the outer loop, and ((num = inScan.nextInt()) != -999) as the continuation condition for the inner loop.