Java Program

Java Program

I need a code that

Read the integers from the file into an ArrayList object that is initially created with the default ArrayList constructor.

Compute the average of the integers from the input file.

Compute the sum of the integers from the input file that have even values.

Print a formatted report with labeled output with the following:

Print the number of integers read from the file.

Print the average of the integers read from the file.

Print the sum of the integers read the file that have even values.

Print a list of the integers read from the file.

Search through the ArrayList for a values of 31 and replace all of them with the value 3100.

Add the integers -1, -2, -3, -4, -5 to the beginning of the ArrayList.

Add the integers 101, 102, 103, 104 to the end of the ArrayList.

Continue the formatted report with the following:

Print the list of the integers in the ArrayList after the modifications from items 5, 6, and 7 are made.

Expert Answer



  • 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.util.ArrayList;
import java.util.Scanner;

public class Numbers {
public static void main(String[] args)throws Exception{
* 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.

/*Here numCount is incremented by 1*/
* value is added to totalSum below.

* Now suppose value is even then we add value into evenSum.
* value is even when remainder is 0 when value is divided by 2
* Mod operator(%) is used to get remainder.
}//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___________”);

* 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) {
* 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::”);

* 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::”);

* 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::”);
System.out.println(“n___________________Final status of the list_____________________”);
}catch(FileNotFoundException e){
System.out.println(“Please check name of the file again.”);
} // MAin ends
}//class ends

Text file input.txt data::


Buy Essay
Calculate your paper price
Pages (550 words)
Approximate price: -

Help Me Write My Essay - Reasons:

Best Online Essay Writing Service

We strive to give our customers the best online essay writing experience. We Make sure essays are submitted on time and all the instructions are followed.

Our Writers are Experienced and Professional

Our essay writing service is founded on professional writers who are on stand by to help you any time.

Free Revision Fo all Essays

Sometimes you may require our writers to add on a point to make your essay as customised as possible, we will give you unlimited times to do this. And we will do it for free.

Timely Essay(s)

We understand the frustrations that comes with late essays and our writers are extra careful to not violate this term. Our support team is always engauging our writers to help you have your essay ahead of time.

Customised Essays &100% Confidential

Our Online writing Service has zero torelance for plagiarised papers. We have plagiarism checking tool that generate plagiarism reports just to make sure you are satisfied.

24/7 Customer Support

Our agents are ready to help you around the clock. Please feel free to reach out and enquire about anything.

Try it now!

Calculate the price of your order

Total price:

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.


Let us write that nagging essay.


Submit Your Essay/Homework Instructions

By clicking on the "PLACE ORDER" button, tell us your requires. Be precise for an accurate customised essay. You may also upload any reading materials where applicable.


Pick A & Writer

Our ordering form will provide you with a list of writers and their feedbacks. At step 2, its time select a writer. Our online agents are on stand by to help you just in case.


Editing (OUR PART)

At this stage, our editor will go through your essay and make sure your writer did meet all the instructions.


Receive your Paper

After Editing, your paper will be sent to you via email.

× How can I help you?