java as a beginner
Solution
//import statements
import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;
public class Calculation{
//main method
public static void main(String[] args) {
//try so that we can catch file input exception
try{
//taking input from file called numbers.txt
Scanner scanner = new Scanner(new File("numbers.txt"));
//declaring numbers arraylist and not array since we don't know the number of integers
present in the file
ArrayList numbers = new ArrayList();
//reading integers from file and adding it to numbers arraylist
while(scanner.hasNextInt()){
numbers.add(scanner.nextInt());
}
//initialising sum
int sum = 0;
//iterating through the list and adding the number
for (int i=0; i

java as a beginnerSolutionimport statements import java.io.pdf

  • 1.
    java as abeginner Solution //import statements import java.io.*; import java.util.Scanner; import java.util.ArrayList; public class Calculation{ //main method public static void main(String[] args) { //try so that we can catch file input exception try{ //taking input from file called numbers.txt Scanner scanner = new Scanner(new File("numbers.txt")); //declaring numbers arraylist and not array since we don't know the number of integers present in the file ArrayList numbers = new ArrayList(); //reading integers from file and adding it to numbers arraylist while(scanner.hasNextInt()){ numbers.add(scanner.nextInt()); } //initialising sum int sum = 0; //iterating through the list and adding the number for (int i=0; i