Question

In: Computer Science

Write a program which reads an input file. It should assume that all values in the...

Write a program which reads an input file. It should assume that all values in the input file are integers written in decimal. Your program should read all integers from the file and print their sum, maximum value, minimum value, and average. Use the FileClient class here (from a previous reading) as an example. You'll need to create a file to be used as input to test your program, as well. Your program should work whether the integers are separated by spaces, new lines, tabs or a mix of the three (but it does not need to work with other delimiters, like commas). In other words, it should work as long as the integers are separated only by white space characters (this is what the Scanner's next<primitive> functions do).

To convert individual integers written in the file from their string from to int form, find and use an appropriate method in the Integer wrapper class.

import java.io.File;
import java.util.Scanner;

class FileClient
{
    public static void main(String[] args) throws java.io.FileNotFoundException
    {
        File inputFile = new File("input.txt");
        Scanner inputFileScanner = new Scanner(inputFile);

        String nextWord = "";

        boolean keepLooping = inputFileScanner.hasNext();
        while ( keepLooping )
        {
            nextWord = inputFileScanner.next();
            System.out.println( nextWord );
            keepLooping = inputFileScanner.hasNext();
        }
    }
}

Solutions

Expert Solution

import java.io.File;
import java.util.Scanner;

public class FileClient
{
public static void main(String[] args) throws java.io.FileNotFoundException
{
File inputFile = new File("input.txt");
Scanner inputFileScanner = new Scanner(inputFile);

String nextWord = "";
int sum=0,min=Integer.MAX_VALUE,max=-1,count=0;
boolean keepLooping = inputFileScanner.hasNext();
while ( keepLooping )
{
nextWord = inputFileScanner.next();
System.out.println( nextWord );
int n =Integer.valueOf(nextWord.trim());
sum+=n;
count++;
if(n>max)
   max=n;
if(n<min)
   min=n;
keepLooping = inputFileScanner.hasNext();
}
double avg = sum/(double)count;
System.out.println("Sum: "+sum);
System.out.println("Avg: "+avg);
System.out.println("Max: "+max);
System.out.println("Min: "+min);
}
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

Write a program that reads in characters until end of file. The program should count and...
Write a program that reads in characters until end of file. The program should count and print the number of characters, printable characters, vowels, digits, and consonants in the input. Use functions to check whether a character is a vowel, a consonant, or a printable character. Define and use macros to test if a character is a digit or a letter.
Write a program that reads a file called document.txt which is a text file containing an...
Write a program that reads a file called document.txt which is a text file containing an excerpt from a novel. Your program should print out every word in the file that contains a capital letter on a new line to the stdout. For example: assuming document.txt contains the text C++
Write a program that reads two strings from an input file (The first line is X,...
Write a program that reads two strings from an input file (The first line is X, the second line is Y), compute the longest common subsequence length AND the resulting string. You will need to write 2 methods 1) return LCS length in iterative function // return the length of LCS. L is the 2D matrix, X, Y are the input strings, m=|X|, n=|Y| int lcs_it(int **C, string X, string Y, int m, int n ) 2) return LCS resulting...
Write a Python program that reads a file, input by the user, containing one word/token per...
Write a Python program that reads a file, input by the user, containing one word/token per line with an empty line between sentences. The program prints out the longest word found in the file along with its length.
2. Write a Java program that reads a series of input lines from given file “name.txt”,...
2. Write a Java program that reads a series of input lines from given file “name.txt”, and sorts them into alphabetical order, ignoring the case of words. The program should use the merge sort algorithm so that it efficiently sorts a large file. Contents of names.text Slater, KendallLavery, RyanChandler, Arabella "Babe"Chandler, StuartKane, EricaChandler, Adam JrSlater, ZachMontgomery, JacksonChandler, KrystalMartin, JamesMontgomery, BiancaCortlandt, PalmerDevane, AidanMadden, JoshHayward, DavidLavery,k JonathanSmythe, GreenleeCortlandt, OpalMcDermott, AnnieHenry, DiGrey, MariaEnglish, BrookeKeefer, JuliaMartin, JosephMontgomery, LilyDillon, AmandaColby, LizaStone, Mary FrancesChandler, ColbyFrye, DerekMontgomery,...
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
● Write a program that reads words from a text file and displays all the words...
● Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. Must use ArrayList. MY CODE IS INCORRECT PLEASE HELP THE TEXT FILE CONTAINS THESE WORDS IN THIS FORMAT: drunk topography microwave accession impressionist cascade payout schooner relationship reprint drunk impressionist schooner THE WORDS MUST BE PRINTED ON THE ECLIPSE CONSOLE BUT PRINTED OUT ON A TEXT FILE IN ALPHABETICAL ASCENDING ORDER...
Write a C++ program that reads integers from standard input until end of file. Print out...
Write a C++ program that reads integers from standard input until end of file. Print out the largest integer that you read in, on a line by itself. Any erroneous input (something that is not an integer) should be detected and ignored. In the case where no integers are provided at all, print NO INTEGERS and stop. The whole program is under 40 lines of code. Just read from cin. Now, you need to detect non integers. In this case,...
Write a program that reads a file (provided as attachment to this assignment) and write the...
Write a program that reads a file (provided as attachment to this assignment) and write the file to a different file with line numbers inserted at the beginning of each line. Such as Example File Input: This is a test Example File Output 1. This is a test. (Please comment and document your code and take your time no rush).
3. Write a Java program that discover all anagrams of all wordslisted in the input file,...
3. Write a Java program that discover all anagrams of all wordslisted in the input file, “dict.txt”. An anagram of a work is a rearrangement of its letters into a new legal word. Your program should do the following: a. Read in the given “dict.txt” file and sort it in each word’s canonical form. The canonical form of a word contains the same letters as the original word, but in sorted order b. Instead of putting the “dict.txt” in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT