Search code examples
javamethodstext-files

How to calculate the sum of a text file using methods?


Assuming that the text file reads: 11 3 6 9 10 7

I am attempting to return the sum of these numbers in the file. Currently, I am returning the listed digits followed by a "0". Not sure where else to take it from here.

Additionally, I would prefer the code be altered from what it is now (I understand that there are BufferReaders, etc., but the assignment I have for class calls that I do the method specifically like this).

package module4labs;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;

public class Module4Labs {
public static void main(String[] args) {
    new Module4Labs();
}

public Module4Labs() {
        try {
        File outFile = new File("Problem1.txt");
        FileOutputStream outFileStream = new 
FileOutputStream(outFile);
        PrintWriter pw = new PrintWriter(outFileStream);
        for (int x=0; x<5; x++) {
            pw.print((int)(Math.random()*10)+1 + " ");
        }
        pw.print(Problem1(outFile));
        pw.close();
    }
    catch (FileNotFoundException e){
    }
        try {
            Scanner input = new Scanner(new File("Problem1.txt"));
            while (input.hasNextLine()) {
                System.out.println(input.nextLine());
            }

        } catch (FileNotFoundException e) {
            System.out.println("File Not Found.");
        }   
}
public int Problem1(File file) throws FileNotFoundException {
    int sum =0;
    try {
    Scanner input = new Scanner(new File("Problem1.txt"));  
while(input.hasNextInt()) {
    sum+=input.nextInt();
}
    }
    catch(FileNotFoundException e) {
        System.out.println("File Not Found.");
    }
return sum;
}

Solution

  • Alrighty, so I found the EXACT answer I was looking for (though I'm sure there are some variations like the one above by Mr. Polywhirl).

    package module4labs;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.PrintWriter;
    import java.util.Scanner;
    
    public class Module4Labs {
    public static void main(String[] args) {
        new Module4Labs();
    }
    
    public Module4Labs() {
            try {
            File outFile = new File("Problem2.txt");
            FileOutputStream outFileStream = new FileOutputStream(outFile);
            PrintWriter pw = new PrintWriter(outFileStream);
            for (int x=0; x<4; x++) {
                pw.print((int)(Math.random()*10)+1 + ", ");
            }
            pw.print((int)(Math.random()*10));
            pw.println();
            pw.close();
            System.out.println("Sum: "+Problem2(outFile));
        }
        catch (FileNotFoundException e){
            System.out.println("You a dunce");
        }
            try {
                Scanner input = new Scanner(new File("Problem2.txt"));
                while (input.hasNextLine()) {
                    System.out.println(input.nextLine());
                }
    
            } catch (FileNotFoundException e) {
                System.out.println("File Not Found.");
            }       
     }
    
     //Problem 1: Sum of all numbers in a line
     public int Problem1(File file) throws FileNotFoundException {
        int sum =0;
        try {
        Scanner input = new Scanner(file);
        while(input.hasNextInt()) {
            sum+=input.nextInt();
        }
        input.close();
     } catch (FileNotFoundException e) {
        System.out.println(file + "File Not Found.");
        }
     return sum;
     }