Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

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.

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){}

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions