Objective: Text File I/0 and Regular Expressions Note that both classes below sh
ID: 3729715 • Letter: O
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.Explanation / Answer
/***** I have tested the code in Eclipse. To test the Class1 you must provide the command line arguments. First argument will be the filename and second argument will be number which you want to enter ****/
public class Class1 {
public static void main(String args[]){
if(args.length < 2){
System.out.println("InCorrect usage : Please enter FileName and number as command line argument");
return;
}
String fileName = args[0];
BufferedWriter bw = null;
FileWriter fw = null;
try{
int num = Integer.parseInt(args[1]);
fw = new FileWriter(fileName);
bw = new BufferedWriter(fw);
Random rand = new Random(152);
for(int i=0 ; i< num; i++){
int pickedNumber = rand.nextInt(1000) + 1;
bw.write(Integer.toString(pickedNumber));
if( (i + 1)%5 > 0 && (i != num -1)){
bw.write("&");
}else{
bw.write("#");
bw.newLine();
}
}
}catch(NumberFormatException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
try {
if (bw != null)
bw.close();
if (fw != null)
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
/****** Second Class is below. To test this you must provide the file name which you want to read as command line argument ****/
public class Class2{
public static void main(String args[]){
if(args.length < 1){
System.out.println("InCorrect usage : Provide the file name as command line argument");
}
try {
File file = new File("E:\output2.txt");
Scanner in = new Scanner(file);
int total = 0;
int count = 0;
while (in.hasNextLine()) {
String line = in.nextLine();
String [] str = line.split("&|\#");
count += str.length;
for(String st : str){
total += Integer.parseInt(st);
}
}
System.out.println("Count is: " + count);
System.out.println("Average is: " + total/count);
in.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.