To receive full credit on this assignment you should use a w

To receive full credit on this assignment, you should use a while loop that is not pre-programmed to know how many numbers there are in the file. But if you can\'t get the while loop version running, you may a for loop for (-1) credit.

This is the link of TXT.

https://drive.google.com/open?id=0B1IThl7l1PdKOUFhS3RQTnBaV2c

And use JAVA

Solution

Code:


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Main {

   public static void main(String[] args) {
       int sum = 0;
       int n;
       BufferedReader br = null;

       try {

           String sCurrentLine;

           br = new BufferedReader(new FileReader(\"input.txt\"));

           while ((sCurrentLine = br.readLine()) != null) {
               try{
                   n = Integer.parseInt(sCurrentLine);
                   sum = sum + n;
               }catch(NumberFormatException e){
                  
               }
           }

       } catch (IOException e) {
           e.printStackTrace();
       } finally {
           try {
               if (br != null)br.close();
           } catch (IOException ex) {
               ex.printStackTrace();
           }
       }
       System.out.println(\"The sum is: \"+sum);
   }
}

Output:

To receive full credit on this assignment, you should use a while loop that is not pre-programmed to know how many numbers there are in the file. But if you can

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site