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

Can someone assist me with this? Write a program ReadAllDoubles.java that reads

ID: 3664883 • Letter: C

Question

Can someone assist me with this?

Write a program ReadAllDoubles.java that reads an entire text file, and adds up any numbers encountered on the input, then prints the sum at the end. The name of the file should be read from a keyboard Scanner. Big hint: you should continue reading the file as long as any input remains (that is, hasNextreturns true), and for each item, if it can be read as a double value, read it as one (nextDouble) and add it to your sum. If it can't, read it as a String (next) and ignore it.

Thank You

Explanation / Answer

package com.he.capillary;

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

public class ReadFile {

   public static void main(String[] args) throws FileNotFoundException {
       double totalValue = 0.0d;
       Scanner scanner = new Scanner(System.in);
       String fileName = scanner.nextLine();
       try{
           scanner = new Scanner(new File(fileName));
           while(scanner.hasNextLine()){
               if(scanner.hasNextDouble()){
                   totalValue += scanner.nextDouble();
               }else{
                   scanner.nextLine();
               }
           }
           System.out.println(totalValue);
       }catch(Exception exception){
           System.out.println("File Not found.");
       }
   }
}