68 780
Assignments Done
Successfully Done
In January 2019

Answer to Question #59510 in Java | JSP | JSF for James

Question #59510
Write a java program that will do the following. Use classes and methods as needed.

1. Prompt the user to input five integers.
2. Put the integers into an array named UserNums
3. Display the sum of the numbers and the average.
4. Write the numbers out to a file named UserNums.txt. Use a relative path so the file will be saved in your project folder.
Expert's answer
package com.company;

import java.io.*;

public class Main {
public static void WriteFile(Integer[] array) throws IOException
FileWriter filewriter = new FileWriter(new File("UserNums.txt"));
for (int i = 0; i < array.length;i++)
filewriter.write(array[i] + " ");
public static void main(String[] args) throws Exception
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Input 5 integers");
Integer[] UserNums = new Integer[5];
int sum = 0;
int len = UserNums.length;
for (int i = 0; i < len; i++)
UserNums[i] = Integer.parseInt(reader.readLine());
sum += UserNums[i];
double average = sum / len;
System.out.println("sum = " + sum);
System.out.println("average = " + average);

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!


No comments. Be first!

Leave a comment

Ask Your question

Privacy policy Terms and Conditions