Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I only get output as Need 2 arguments -inputfile and outputfile with below code. I need to get an output like sample. Could you

I only get output as "Need 2 arguments -inputfile and outputfile " with below code. I need to get an output like sample. Could you help me fix this code please?

ProcessTokens.java

import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.nio.file.Files; import java.util.ArrayList; import java.util.Scanner;

public class ProcessTokens { private ArrayList alphaTokens; private ArrayList intTokens; private ArrayList doubleTokens; private ArrayList otherTokens; private ArrayList allTokens; private int nonEmptyLines; private int otherchars; private int digits; private int letters; private String longestAlphaToken; private double largestNumeric ; private String inFilename, outFilename; public ProcessTokens(String inFname, String outFname) { this.inFilename = inFname; this.outFilename = outFname; allTokens = new ArrayList(); alphaTokens = new ArrayList(); intTokens = new ArrayList(); doubleTokens = new ArrayList(); otherTokens = new ArrayList(); longestAlphaToken=""; } public void parse() throws IOException { Scanner fileScanner = new Scanner(new File(inFilename)); while(fileScanner.hasNextLine()) { String line = fileScanner.nextLine(); //if(line.trim().equals("")) //continue; nonEmptyLines++; Scanner lineScanner = new Scanner(line); String token; while(lineScanner.hasNext()) { token = lineScanner.next(); allTokens.add(token); if(token.matches("[a-zA-z]+")) // letters only { alphaTokens.add(token); if(token.length() > longestAlphaToken.length()) longestAlphaToken = token; letters += token.length(); } else if(token.matches("[0-9]+")) //int token { intTokens.add(token); digits += token.length(); Integer num = Integer.parseInt(token); if(num > largestNumeric) largestNumeric = num; } else if(token.matches("[0-9]+\\.[0-9]+")) { Double num = Double.parseDouble(token); doubleTokens.add(token); digits += token.length() - 1; //count all but the decimal point otherchars++; //for the decimal point if(num > largestNumeric) largestNumeric = num; } else { // not a double value otherTokens.add(token); char ch; for(int i = 0; i

MyCounts5.java (driver)

import java.io.IOException;

public class MyCounts5 {

public static void main(String[] args) {

if(args.length != 2)

{

System.out.println("Need 2 arguments - inputfile and outputfile");

System.exit(1);

}

ProcessTokens counter = new ProcessTokens(args[0], args[1]);

try {

counter.parse();

counter.printStats();

} catch (IOException e) {

System.out.println(e.getMessage());

}

}

}

image text in transcribed

-----------------------------------------------------------------------------------------------------------------

data5.txt (Sample input)

image text in transcribed

--------------------------------------------------------------------------------------------------------------------image text in transcribedimage text in transcribed

Objectives: a. Using input and output text files b. Using the Scanner class c. Using ArrayList d. Using the String split, matches, and other methods e. Using some of the Character class methods- f. Using regular Expressions- g. Using Exceptions. h. Using Command-line arguments- I. 41 Write a program that reads data from a text file and accomplishes the following: 1. Display the alphabetic tokens on ascending order with their number of occurrences. 2. Display the valid integer values 3. Scan each line for social security numbers in the form ssn999-99-9999. Display all valid social securitv numbers. 4. Scan each line for timestamps in the form of time-9999ms. When done, display the total time in ms in the file. 9 represents a digit and time may have any number of digits.* Input text file and output text file should be entered as command-line arguments (input file . name). You should read the input file iust one time, A part of the grade will be assigned based on the efficiency . Program should display output on the screen. Each team submits a single copy of project zipped into a single file, additionally, each team member submits her/his project report. Each team should submit the following items: a. MyCounts5.java (driver). Driver should be short and simple. b. All other class needed files.+ c. MySampleRun5 Samnle input. CS 1302 A 2/18/2019 Fares An Exception can be anything which interrupts the normal flow of the program. When an exception occurs program processing gets terminated and doesn't continue further. In such cases we get a system generated error message The good thing about exceptions is that they can be handled Testl 89.5 Test2 100 Test3 55.75v Final Quiz 100.50.759 25. When are we going to have a test? Are the PPT slides helpful?* You have done a good job!!! RegularExpressions Often, you need to write code time-lms to validate user input time-12ms ssn23-345-8765 time 123ms ssn23-ABC-8765 time-12Xms ssn987-65-4321 time 321ms ssn123-45-6789 ssn34-455-98761 time 1000ms ABCssn345-55-4321xyz- Abctime-lams 1234 ample output II.1. Sorted alphabetic tokens with their frequencies 1 A 2 An 3 Are 4 CS 5 Exception 6 Fares 7 Final 8 In 9 Often 10 PPT 11 Quiz 12 RegularExpressions 1 13 The 14 Whern 15 You 16 a 17 about 18 an 19 and 20 anything 21 are 22 be 23 can 24 cases 25 code 26 continue 27 done 28 error 29 exception 30 exceptions 31 flow 32 further 33 generated 34 get 35 gets 36 going 37 good 38 handled 39 have 40 helpful 41 input 42 interrupts 43 1S 44 message 45 need 46 normal 47 occurs 48 of 49 processing 50 program 51 slides 52 such 53 system 54 terminated 55 test 56 that 57 the 58 they 59 thing 60 to 61 user 62 validate 63 we 64 which 65 write 66 you II.2 Integer tokens 1302 18. 2019 4 1234 II.3 Valid Social Security Numbers 987-65-4321 123-45-6789 345-55-4321 II.4. Valid Times and total 12 123 321 1000 2 4 Total Time is 1457

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Programming The Perl DBI Database Programming With Perl

Authors: Tim Bunce, Alligator Descartes

1st Edition

1565926994, 978-1565926998

More Books

Students also viewed these Databases questions