Explanation::
- Code in JAVA is given below.
- Please read all the comments provided in the code for better understanding of the code.
- Please see that text file is in the same folder where the java file is located. Or in the same project folder if using IDE like netbeans.
- Every step is explained in detail.
- Output is provided in screenshot format.
- Note:: Class name is Numbers.
Code in JAVA::
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Numbers {
public static void main(String[] args)throws Exception{
try{
/**
* ArrayList of type Integer is created and is named as numbers.
* All the integers that are taken from file are stored in numbers
* ArrayList.
*/
ArrayList<Integer> numbers=new ArrayList<Integer>();
/*
* To take input from a text file we need to use File class.
* Text file name is passed as parameter to this class as seen below.
* In our case file name is input.txt
* File class object is named as input.
*/
/*
* Mine text file name is input.txt so please put correct name of the file.
*
*/
File input = new File(“input.txt”);
/*
* Scanner class is used to scan string lines from the text file.
* Scanner class object is named as fr.
*/
Scanner fr = new Scanner(input);
/**
* Following I have declared and initialized three integer variables.
* 1. numCount stores how many numbers are taken from file.
* 2. evenSum stores sum of integers that are even.
* 3. totalSum stores summation of all the values in file.
*/
int numCount=0,evenSum=0,totalSum=0;
/**
* This while loop iterates until there are no numbers in the text file.
* Once the last line is scanned, the loop terminates.
* hasNextLine() returns true if there is another line in
* file to scan.
*
*/
while (fr.hasNextLine()) {
/*
* If there is line in the file then we
* scan that line using fr.readLine();
* And store that value as a string in variable named s
*/
String s=fr.nextLine();
/*
* Scanned string s is converted to integer value below.
* This value is stored in integer variable named value.
*/
int value=Integer.parseInt(s);
/*
* Now we add the value into ArrayList numbers using add() function
* of ArrayList.
*/
numbers.add(value);
/*Here numCount is incremented by 1*/
numCount++;
/*
* value is added to totalSum below.
*/
totalSum=totalSum+value;
/*
* Now suppose value is even then we add value into evenSum.
*/
if(value%2==0){
/*
* value is even when remainder is 0 when value is divided by 2
* Mod operator(%) is used to get remainder.
*/
evenSum+=value;
}
}//while Loop ends here
/*
* Following we print all the data as required.
*/
System.out.println(“________________________ Printing Data ____________________________”);
System.out.println(“Number of integers read from the file ::”+numCount);
System.out.println(“Average of the numbers is ::”+((double)totalSum/(double)numCount));
System.out.println(“Sum of even integers in file ::”+evenSum);
System.out.println(“n__________ LIST OF INTEGERS READ FROM FILE___________”);
System.out.println(numbers);
/*
* Now after all the file work is done.
* Next work is to find 31 and replace it with 3100.
* Following for loop iterates through ArrayList numbers
* And in each check if current number is 31 or not.
* If it is 31 then using indexOf() and set() functions we replace
* 31 with 3100.
*/
for (Integer num : numbers) {
if(num==31){
/*
* set() function takes two parameter.
* First parameter is the position of the ArrayList where we need to put the new value.
* Second parameter is the new value i.e 3100.
* To get index of current position we use indexOf() function
*/
numbers.set(numbers.indexOf(num), 3100);
}
}
/*
* After replacement we print again the list
*/
System.out.println(“nAfter replacement::”);
System.out.println(numbers);
/*
* Adding values at the beginning. Note that I have added numbers
* -1,-2,-3,-4,-5 in the order of -1 -2 -3 -4 -5 ….
* To get the order -5 -4 -3 -2 -1, replace the first parameter of
* add(i,value) function i.e i to 0.
* To get -1 -2 -3 -4 -5 .. pattern see how I have used i values as
* 0,1,2,3,4 respectively.
*/
System.out.println(“nAdding values -1 -2 -3 -4 -5::”);
numbers.add(0,-1);
numbers.add(1,-2);
numbers.add(2,-3);
numbers.add(3,-4);
numbers.add(4,-5);
/*
* See how the add(i,value) function uses i=numbers.size()
* numbers.size() gives the position where we need to add the value.
*/
System.out.println(“nAdding values 101 102 103 104 at the end::”);
numbers.add(numbers.size(),101);
numbers.add(numbers.size(),102);
numbers.add(numbers.size(),103);
numbers.add(numbers.size(),104);
System.out.println(“n___________________Final status of the list_____________________”);
System.out.println(numbers);
fr.close();
}catch(FileNotFoundException e){
System.out.println(“Please check name of the file again.”);
}
} // MAin ends
}//class ends
Text file input.txt data::

Output::
