Question

Objective: Text File I/0 and Regular Expressions Note that both classes below should handle all exceptions that might be thrown within them. 1. Create a class that does the following: a. Reads the name of a file to create as the first command line argument. (Overwrite any file with the same name). b. Reads an integer value as the second command line argument. C. The program should generate as many random numbers as are specified in the second command line argument (from step b) with magnitudes between 1 and 1000 (both inclusive) and write them to the file. Use a seed of 152 for your random number generator. d. You must write only 5 numbers on each line and then begin a new line. (The last line may have less than 5 numbers.) Delimit the numbers on a line with the & character. The end of a line should be delimited by the # character. No other delimiters should be explicitly used. A sample output file for 12 numbers might look like this: 19834&4&235&6# 1278524840085&611# 55&2# 2. Create a second class that does the following: a. Reads the name of the file to open as a command line argument. (Check for incorrect usage). b. Reads in all of the numbers from that file using a Scanner. (Assume a file format identical to the file created in (1) and make sure you use regular expressions to specify the delimiter.) c. Displays, to the standard output, the count and average of all of those numbers.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below are the Java programs to create a file having random numbers (FileWrite.java) and read that file and display count and average of the numbers(FileReadScanner.java).

Copy the below files in your editor and test.

FileWrite.java

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStreamWriter;

import java.util.Random;

public class FileWrite {

public static void main(String args[]) throws IOException {

String fileName = args[0];

int numCount = Integer.parseInt(args[1]);

File fout = new File(fileName);

FileOutputStream fos = new FileOutputStream(fout);

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));

int i = 0;

while (i < numCount) {

int j = 0;

StringBuffer buffer = new StringBuffer();

while (j < 5 && i< numCount) {

int randomNum = getRandomNumberInRange(1, 1000);

buffer.append(randomNum);

if (j != 4 && i != numCount -1)

buffer.append("&");

j++;

i++;

}

buffer.append("#");

bw.write(buffer.toString());

bw.newLine();

}

bw.close();

}

private static int getRandomNumberInRange(int min, int max) {

if (min >= max) {

throw new IllegalArgumentException("max must be greater than min");

}

Random r = new Random();

return r.nextInt((max - min) + 1) + min;

}

}

FileReadScanner.java

import java.io.File;

import java.io.IOException;

import java.util.Scanner;

import java.util.regex.Pattern;

public class FileReadScanner {

public static void main(String args[]) throws IOException {

String fileName;

fileName = args[0];

File file = new File(fileName);

Scanner sc = new Scanner(file);

int count=0,sum =0;

while (sc.hasNextLine() ) {

String line = sc.nextLine();

String[] output = line.split(Pattern.quote("&"));

for(String s: output){

count++;

if(s.contains("#")){

s= s.substring(0, s.length()-1);

}

sum +=Integer.parseInt(s);

System.out.println(s);

}

}

sc.close();

System.out.println("Count of numbers is :" + count);

System.out.println("Average of numbers is :" + (sum/count));

}

}

Output :

623
939
357
193
86
918
469
566
605
Count of numbers is :9
Average of numbers is :528

import java.io.File; import java.io.IOException; import java.util.Scanner; import java.util.regex.Pattern; public class FileR

e> FileReadScanner ava Applic 623 939 357 193 86 918 469 566 605 Count of numbers is:9 Average of numbers is :528

Add a comment
Know the answer?
Add Answer to:
Objective: Text File I/0 and Regular Expressions Note that both classes below should handle all exceptions...
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
  • Regular Expression processor in Java Overview: Create a Java program that will accept a regular expression...

    Regular Expression processor in Java Overview: Create a Java program that will accept a regular expression and a filename for a text file. The program will process the file, looking at every line to find matches for the regular expression and display them. Regular Expression Format The following operators are required to be accepted: + - one or more of the following character (no groups) * - zero or more of the following character (no groups) [] – no negation,...

  • Please write a c++ header file, class implementation file and main file that does all of...

    Please write a c++ header file, class implementation file and main file that does all of the following and meets the requirements listed below. Also include a Output of your code as to show that your program works and functions properly. EXERCISING A DOUBLY-LINKED LIST CLASS This project consists of two parts, the second of which appears below. For the first part, write a class that implements an unordered list abstract data type using a doubly-linked list with pointers to...

  • All commands must be in the command line. The expense data file is separate. Programming Project...

    All commands must be in the command line. The expense data file is separate. Programming Project #2: Manage Spending Using Commands Objectives: • Understand how to create and manipulate list of objects using array or vector class • Handle input errors and invalid values • Design and create a well-structure program using C++ basic programming constructs and classes. Description: Each “expense” contains 2 values: the spending amount (double) and its description (string) Here is an example of the “expense” data...

  • I am trying to read from a file with with text as follows and I am...

    I am trying to read from a file with with text as follows and I am not sure why my code is not working. DHH-2180 110.25 TOB-6851 -258.45 JNO-1438 375.95 CS 145 Computer Science II IPO-5410 834.15 PWV-5792 793.00 Here is a description of what the class must do: AccountFileIO 1. Create a class called AccountFileIO in the uwstout.cs145.labs.lab02 package. (This is a capital i then a capital o for input/output.) a. This class will read data from a file...

  • For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java...

    For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java program that will input a file of sentences and output a report showing the tokens and shingles (defined below) for each sentence. Templates are provided below for implementing the program as two separate files: a test driver class containing the main() method, and a sentence utilities class that computes the tokens and shingles, and reports their values. The test driver template already implements accepting...

  • A. File I/O using C library functions File I/O in C is achieved using a file...

    A. File I/O using C library functions File I/O in C is achieved using a file pointer to access or modify files. Processing files in C is a four-step process: o Declare a file pointer. o Open the desired file using the pointer. o Read from or write to the file and finally, o Close the file. FILE is a structure defined in <stdio.h>. Files can be opened using the fopen() function. This function takes two arguments, the filename and...

  • Write a c++ program in that file to perform a “Search and Replace All” operation. This...

    Write a c++ program in that file to perform a “Search and Replace All” operation. This operation consists of performing a case-sensitive search for all occurrences of an arbitrary sequence of characters within a file and substituting another arbitrary sequence in place of them. Please note: One of the biggest problems some students have with this exercise occurs simply because they don’t read the command line information in the course document titled “Using the Compiler’s IDE”. Your program: 1. must...

  • Random accesses to a file. A file contains a formatted list of 9999 integers that are...

    Random accesses to a file. A file contains a formatted list of 9999 integers that are randomly generated in the range of [1,9999]. Each integer occupies one single line and takes 4 characters' space per line. Alternatively, you can think that each number takes 5 characters' space, four for the number and one for the newline character. Write a C++ program using the seekg() and seekp() functions to insert the numbers 7777 through 7781 between the 6000-th and 6001-st numbers...

  • What this Assignment Is About: Review on Java I topics, such as primitive data types, basic...

    What this Assignment Is About: Review on Java I topics, such as primitive data types, basic I/O, conditional and logical expressions, etc. Review on Java loops. Documentation Requirements to get full credits in Documentation The assignment number, your name, StudentID, Lecture number(time), and a class description need to be included at the top of each file/class. A description of each method is also needed. Some additional comments inside of methods (especially for a "main" method) to explain code that are...

  • Need help with java programming. Here is what I need to do: Write a Java program...

    Need help with java programming. Here is what I need to do: Write a Java program that could help test programs that use text files. Your program will copy an input file to standard output, but whenever it sees a “$integer”, will replace that variable by a corresponding value in a 2ndfile, which will be called the “variable value file”. The requirements for the assignment: 1.     The input and variable value file are both text files that will be specified in...

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