Question

Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file...

Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file doesn’t exist. You will process through the file skipping any text or real (double) numbers. You will print the max, min, sum, count, and average of the integers in the file. You will want to create test files that contain integers, doubles, and Strings. HINT: Use hasNextInt() method and while loop. You may also want to use Integer.MAX_VALUE and Integer.MIN_VALUE for the initialization of your min and max values (https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html) The min/max loop (if/esle statements) may be helpful here.

import java.util.*;
import java.io.*;

/**
* Program reads in a file and find the max, min, sum count, and average of all
* integers in the file
*
*/
public class ProcessFile {

/**
* Starts the program
*
* @param args array of command line arguments (not used)
*/
public static void main(String[] args) throws FileNotFoundException {
userInterface();
}

/**
* Interface with the user
*/
public static void userInterface() throws FileNotFoundException {
Scanner console = new Scanner(System.in);
Scanner fileScanner = getInputScanner(console);

// need to declare (and initalize) variables
// int variables: max, min, sum, count
// double variable: average

// process file
// only want to examine the integers in the file

System.out.println("Maximum = " + max);
System.out.println("Minimum = " + min);
System.out.println("Sum = " + sum);
System.out.println("Count = " + count);
System.out.println("Averge = " + average);
}

/**
* Reads filename from user until the file exists, then return a file
* scanner
*
* @param console Scanner that reads from the console
* @return a scanner to read input from the file
* @throws FileNotFoundException if File does not exist
*/
public static Scanner getInputScanner(Scanner console) throws FileNotFoundException {
System.out.print("Enter a file name to process: ");
File file = new File(console.next());
while (!file.exists()) {
System.out.print("File doesn't exist. " + "Enter a file name to process: ");
file = new File(console.next());
}

Scanner fileScanner = new Scanner(file);
return fileScanner;
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.*;
import java.io.*;

/**
 * Program reads in a file and find the max, min, sum count, and average of all
 * integers in the file
 */
public class ProcessFile {

    /**
     * Starts the program
     *
     * @param args array of command line arguments (not used)
     */
    public static void main(String[] args) throws FileNotFoundException {
        userInterface();
    }

    /**
     * Interface with the user
     */
    public static void userInterface() throws FileNotFoundException {
        Scanner console = new Scanner(System.in);
        Scanner fileScanner = getInputScanner(console);

// need to declare (and initalize) variables
// int variables: max, min, sum, count
// double variable: average
        int max = Integer.MIN_VALUE, min = Integer.MAX_VALUE, sum = 0, count = 0, num;
        double average;
        while (fileScanner.hasNextInt()) {
            num = fileScanner.nextInt();
            if (num > max) {
                max = num;
            }
            if (num < min) {
                min = num;
            }
            sum += num;
            count++;
        }
        average = sum / (double) count;
// process file
// only want to examine the integers in the file

        System.out.println("Maximum = " + max);
        System.out.println("Minimum = " + min);
        System.out.println("Sum = " + sum);
        System.out.println("Count = " + count);
        System.out.println("Averge = " + average);
    }

    /**
     * Reads filename from user until the file exists, then return a file
     * scanner
     *
     * @param console Scanner that reads from the console
     * @return a scanner to read input from the file
     * @throws FileNotFoundException if File does not exist
     */
    public static Scanner getInputScanner(Scanner console) throws FileNotFoundException {
        System.out.print("Enter a file name to process: ");
        File file = new File(console.next());
        while (!file.exists()) {
            System.out.print("File doesn't exist. " + "Enter a file name to process: ");
            file = new File(console.next());
        }

        Scanner fileScanner = new Scanner(file);
        return fileScanner;
    }
}

Add a comment
Know the answer?
Add Answer to:
Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Finish FormatJavaProgram.java that prompts the user for a file name and assumes that the file contains...

    Finish FormatJavaProgram.java that prompts the user for a file name and assumes that the file contains a Java program. Your program should read the file (e.g., InputFile.java) and output its contents properly indented to ProgramName_Formatted.java (e.g., InputFile_Formatted.java). (Note: It will be up to the user to change the name appropriately for compilation later.) When you see a left-brace character ({) in the file, increase your indentation level by NUM_SPACES spaces. When you see a right-brace character (}), decrease your indentation...

  • import java.util.Scanner; // TASK #1 Add the file I/O import statement here /** This class reads...

    import java.util.Scanner; // TASK #1 Add the file I/O import statement here /** This class reads numbers from a file, calculates the mean and standard deviation, and writes the results to a file. */ public class StatsDemo { // TASK #1 Add the throws clause public static void main(String[] args) { double sum = 0; // The sum of the numbers int count = 0; // The number of numbers added double mean = 0; // The average of the...

  • Homework Assignment on Mimir: Read in a file "numbers.txt" into a Java program. Sum up all...

    Homework Assignment on Mimir: Read in a file "numbers.txt" into a Java program. Sum up all the even numbers over 50. Print the result to the console using System.out.println(); The file will only have numbers. Code: import java.io.FileNotFoundException; import java.io.FileReader; import java.util.Scanner; /** * * @author David */ public class ReadingFiles {     /**      * @param args the command line arguments      */     public static void main(String[] args) throws FileNotFoundException {         // TODO code application logic here...

  • Hello, Could you please input validate this code so that the code prints an error message...

    Hello, Could you please input validate this code so that the code prints an error message if the user enters in floating point numbers or characters or ANYTHING but VALID ints? And then could you please post a picture of the output testing it to make sure it works? * Write a method called evenNumbers that accepts a Scanner * reading input from a file with a series of integers, and * report various statistics about the integers to the...

  • I am given an input file, P1input.txt and I have to write code to find the...

    I am given an input file, P1input.txt and I have to write code to find the min and max, as well as prime and perfect numbers from the input file. P1input.txt contains a hundred integers. Why doesn't my code compile properly to show me all the numbers? It just stops and displays usage: C:\> java Project1 P1input.txt 1 30 import java.io.*; // BufferedReader import java.util.*; // Scanner to read from a text file public class Project1 {    public static...

  • 1. Import file ReadingData.zip into NetBeans. Also, please download the input.txt file and store it into...

    1. Import file ReadingData.zip into NetBeans. Also, please download the input.txt file and store it into an appropriate folder in your drive. a) Go through the codes then run the file and write the output with screenshot (please make sure that you change the location of the file) (20 points) b) Write additional Java code that will show the average of all numbers in input.txt file (10 points) import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.io.*; 1- * @author mdkabir...

  • I need help asking the user to half the value of the displayed random number as...

    I need help asking the user to half the value of the displayed random number as well as storing each number generated by the program into another array list and displayed after the game is over at the end java.util.*; public class TestCode { public static void main(String[] args) { String choice = "Yes"; Random random = new Random(); Scanner scanner = new Scanner(System.in);    ArrayList<Integer> data = new ArrayList<Integer>(); int count = 0; while (!choice.equals("No")) { int randomInt =...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • Suppose you define a file format to describe an array of java.awt.Rectangle objects. You start with...

    Suppose you define a file format to describe an array of java.awt.Rectangle objects. You start with an integer saying the length of the array, then a sequence of integers describing the x, y, width, and height parameters that can feed into the constructor for java.awt.Rectangle (according to the documentation). Let's define a method that will return an array of Rectangles given a String representing a filename. You could test the method by creating a file such as this: 2 0...

  • I have this program that works but not for the correct input file. I need the...

    I have this program that works but not for the correct input file. I need the program to detect the commas Input looks like: first_name,last_name,grade1,grade2,grade3,grade4,grade5 Dylan,Kelly,97,99,95,88,94 Tom,Brady,100,90,54,91,77 Adam,Sandler,90,87,78,66,55 Michael,Jordan,80,95,100,89,79 Elon,Musk,80,58,76,100,95 output needs to look like: Tom Brady -------------------------- Assignment 1: A Assignment 2: A Assignment 3: E Assignment 4: A Assignment 5: C Final Grade: 82.4 = B The current program: import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class main {    public static void main(String[]...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT