Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

/* Write a program that reads all the numbers from the file \"mynumbers.txt\" *

ID: 3577017 • Letter: #

Question

/* Write a program that reads all the numbers from the file "mynumbers.txt"
* and prints out the sum of the positive values from the file.
* You must print an error if the file can't be opened or contains any non-numeric values.
* You must write the complete program and output the same result as the one of sample run.
*/

/* sample sun
The sum of the positive values is 165.29.
*/
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;

1e Write a program that reads all the numbers from the file mmynumbers.txt" 2 and prints out the sum of the positive values from the file 3 You must print an error if the file can't be opened or contains any non-numeric values 4 You must write the complete program and output the same result as the one of sample run. 7 sample sun. 8 The sum of the positive values is 165.29. LO import java util. Scanner 11 import java io. Fil 12 import java io Exception 13 import java. util. InputMismatchException. 14 15 public class Findsum 16e public static void main (String args) try (Scanner textfile new Scanner (new File (m 17 umbers .txt //complete the rest of the code. 18 catch (FileNot FoundException ex) 19 catch (InputMismatchException ex) 24 2.5

Explanation / Answer

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;

public class FindSum {
    public static void main(String[] args) {
        double num;
        double sum=0;
        try(Scanner textfile = new Scanner(new File("mynumbers.txt"))){
            //complete the rest of the code.
            while(textfile.hasNextDouble()){
                num = textfile.nextDouble();
                System.out.println(num);
                if(num>0){
                    sum+=num;
                }
            }
        } catch(FileNotFoundException ex){

        } catch(InputMismatchException ex){

        }
        System.out.println(sum);
    }
}