Embed presentation
Download to read offline
![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](https://image.slidesharecdn.com/javaasabeginnersolutionimportstatementsimportjava-230707182441-78f3a57c/75/java-as-a-beginnerSolutionimport-statements-import-java-io-pdf-1-2048.jpg)
The document provides a Java program designed for beginners that reads integers from a file named 'numbers.txt' and calculates their sum. It utilizes an ArrayList to store the integers, allowing for dynamic input size. The program includes error handling for file input exceptions.
![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](https://image.slidesharecdn.com/javaasabeginnersolutionimportstatementsimportjava-230707182441-78f3a57c/75/java-as-a-beginnerSolutionimport-statements-import-java-io-pdf-1-2048.jpg)