Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a java program that will ask a user for any number of positive integers. A

ID: 3874206 • Letter: W

Question

Write a java program that will ask a user for any number of positive integers. Ask them to enter a “0” to end the process. Each number (as long as it is a positive integer) should be saved into a text file called “nums.txt”. If the user enters a floating point or negative number, you should warn them that only positive integers or “0” should be entered. Once the user enters a “0” you are to stop asking for values and close the text file (also, do not save “0” to the text file). Your program should then open the file for reading and print a report that looks like the following chart but, obviously, with the values from the file (note: Chart Average is the average of the chart values):

Total Count:   10
Sum: 154
Max Number:   44
Lowest Number:    2
Average: 15.4
----------------------
Chart Average: 45.1

here is my code ,i have no idea about this one.

package homework1;

import java.io.PrintWriter;

import java.util.Random;

import java.util.Scanner;

import java.io.File;

public class homework1 {

public static PrintWriter pw = null;

public static Scanner fin = null;

public static void main(String[] args){

writeData("nums.txt");

readData("nums.txt");

}

public static void readData(String filename){

int recs=0;

int sum=0;

int maxNumber=0;

int lowestNumber=0;

double average;

double chartAverage;

int val;

try{

fin = new Scanner(new File(filename));

while(fin.hasNextInt()){

val = fin.nextInt();

recs++;

sum= sum+val;

}

while(maxNumber

maxNumber=val;

}

while(lowestNumber>val){

lowestNumber=val;

}

}

catch(Exception e){

System.out.println(e.getMessage());

}finally{

try {

val=0;

fin.close(); } catch(Exception e){}

}

System.out.printf("Total Count: %5d ",recs);

System.out.printf("sum: %5.1f ",sum);

System.out.printf("Max Number: %5d ",maxNumber);

System.out.printf("lowest Number: %5d ",lowestNumber);

System.out.printf("Average: %5d ",sum/(double)recs);

System.out.println("-------------------------------------");

System.out.printf("chart average: %5d ",(recs+sum+maxNumber+lowestNumber+sum/(double)recs)/5.0);

}

public static void writeData (String filename) {

try {

pw = new PrintWriter(new File(filename));

Random rnd = new Random();

for(int i = 0; i < 100; i++) pw.println(rnd.nextInt(101));

} catch(Exception e) {

System.out.println(e.getMessage());

} finally {

try { pw.close(); } catch(Exception e){}

}

}

}

Explanation / Answer


Given below is the code for the question.
Please don't forget to rate the answer if it was helpful. Thank you
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i


package homework1;
import java.io.PrintWriter;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class homework1 {

public static void main(String[] args) {
try {
writeData("nums.txt");
readData("nums.txt");
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
public static void readData(String filename) throws FileNotFoundException{
int recs=0;
int sum=0;
int maxNumber=0;
int lowestNumber=0;
double average;
double chartAverage;
int val;
Scanner fin = new Scanner(new File(filename));
while(fin.hasNextInt()){
val = fin.nextInt();
recs++;
sum= sum+val;
if(val > maxNumber)
maxNumber = val;
if(val < lowestNumber)
lowestNumber = val;
}
fin.close();
average = sum/(double)recs;
chartAverage = (recs+sum+maxNumber+lowestNumber+average)/5.0;
System.out.printf("Total Count: %d ",recs);
System.out.printf("Sum: %d ",sum);
System.out.printf("Max Number: %d ",maxNumber);
System.out.printf("Lowest Number: %d ",lowestNumber);
System.out.printf("Average: %.1f ", average);
System.out.println("-------------------------------------");
System.out.printf("Chart average: %.1f ",chartAverage);
}
public static void writeData(String filename) throws FileNotFoundException {
PrintWriter pw = new PrintWriter(new File(filename));
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter numbers (type 0 to quit):");
while(keyboard.hasNextInt() || keyboard.hasNextDouble())
{
if(keyboard.hasNextInt())
{
int n = keyboard.nextInt();
if(n < 0)
System.out.println("WARNING: Only 0 or +ve integers should be entered.");
else if(n == 0 )
break;
else
pw.println(n);
}
else
{
double d = keyboard.nextDouble();
System.out.println("WARNING: Only integers should be entered.");
}
}
pw.close();
}
}
output
Enter numbers (type 0 to quit):
4
5
9
11.5
WARNING: Only integers should be entered.
-6
WARNING: Only 0 or +ve integers should be entered.
8
1
-2
WARNING: Only 0 or +ve integers should be entered.
0.75
WARNING: Only integers should be entered.
0
Total Count: 5
Sum: 27
Max Number: 9
Lowest Number: 0
Average: 5.4
-------------------------------------
Chart average: 9.3

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote