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.
1
Expert's answer
2016-04-27T10:51:05-0400
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] + " ");
filewriter.flush();
}
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;
WriteFile(UserNums);
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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS