Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Creating a java program that has the following: Objectives: Using input and outp

ID: 3853621 • Letter: C

Question

Creating a java program that has the following:

Objectives:

Using input and output text files

Using the Scanner class

Using ArrayList

Using the String split, matches, and other methods

Using some of the Character class methods

Using regular Expressions

Using Exceptions

Using Command-line arguments

Write a program that reads data from a text file and counts the following:

Number of non-empty lines

Number of tokens

Number of alphabetic tokens (i.e. letters only)

Number of integer tokens

Number of double tokens

Number of other than numeric and alphabetic tokens.

Number of non-white space characters.

Number of digits

Number of letters

Number of other than digits and letters characters

Longest token.

Largest numeric value

Input text file and output text file should be entered as command-line arguments (input file name followed by output file name).

Program should send output to an output text file as well as displaying them 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:

MyCounts5.java (driver)

ProcessTokens.java. this file should process the input file and compute all counts above.

MySampleRun5

Note: Make sure that your program is well documented, readable, and user friendly.

The output is well labeled and aligned

Sample input

CS 1302 A

6 / 22 / 2017

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

Test1    89.5

Test2    100

Test3    55.75

Final          100.50.759

Quiz           25.

When are we going to have a test?

Are the PPT slides helpful?

You have done a good job!!!

Sample output

1.         All tokens

            1          CS

            2          1302

            3          B

            4          6

            5          /

            6          22

            7          /

            8          2017

            9          Fares

            10        An

            11        Exception

            12        can

            13        be

            14        anything

            15        which

            16        interrupts

            17        the

            18        normal

            19        flow

            20        of

            21        the

            22        program.

            23        When

            24        an

            25        exception

            26        occurs

            27        program

            28        processing

            29        gets

            30        terminated

            31        and

            32        doesnÆt

            33        continue

            34        further

            35        In

            36        such

            37        cases

            38        we

            39        get

            40        a

            41        system

            42        generated

            43        error

            44        message

            45        The

            46        good

            47        thing

            48        about

            49        exceptions

            50        is

            51        that

            52        they

            53        can

            54        be

            55        handled

            56        Test1

            57        89.5

            58        Test2

            59        100

            60        Test3

            61        55.75

            62        Final

            63        100.50.759

            64        Quiz

            65        25.

            66        When

            67        are

            68        we

            69        going

            70        to

            71        have

            72        a

            73        test?

            74        Are

            75        the

            76        PPT

            77        slides

            78        helpful?

            79        You

            80        have

            81        done

            82        a

            83        good

            84        job!!!

2.         All Alphabet tokens

            1          CS

            2          B

            3          Fares

            4          An

            5          Exception

            6          can

            7          be

            8          anything

            9          which

            10        interrupts

            11        the

            12        normal

            13        flow

            14        of

            15        the

            16        When

            17        an

            18        exception

            19        occurs

            20        program

            21        processing

            22        gets

            23        terminated

            24        and

            25        continue

            26        further

            27        In

            28        such

            29        cases

            30        we

            31        get

            32        a

            33        system

            34        generated

            35        error

            36        message

            37        The

            38        good

            39        thing

            40        about

            41        exceptions

            42        is

            43        that

            44        they

            45        can

            46        be

            47        handled

            48        Final

            49        Quiz

            50        When

            51        are

            52        we

            53        going

            54        to

            55        have

            56        a

            57        Are

            58        the

            59        PPT

            60        slides

            61        You

            62        have

            63        done

            64        a

            65        good

3.         Double tokens

            1          89.5

            2          55.75

4.         Integer tokens

            1          1302

            2          6

            3          22

            4          2017

            5          100

5.         Other tokens

            1          /

            2          /

            3          program.

            4          doesnÆt

            5          Test1

            6          Test2

            7          Test3

            8          100.50.759

            9          25.

            10        test?

            11        helpful?

            12        job!!!

6          Counts and, largests, and longest

            Count of non-empty lines:             19

            Count of all tokens:                        84

            Count of alphabet tokens:               65

            Count of Integer tokens:                   5

            Count of Double tokens:                   2

            Count of Other tokens:                    12

            Count of numberOfCharacters:          376

            Count of digits:                                     34

            Count of letters:                                  328

            Count of non-digit & letters:                14

            Longest alphabet token:            interrupts

                        Largest numeric value:                2017.00

The text file is as followed:

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

Test1    89.5
Test2   100
Test3   55.75
Final 100.50.759
Quiz 25.

When are we going to have a test?
Are the PPT slides helpful?

You have done a good job!!!

Explanation / Answer

Below is the code needed for the question. Output is also shown. Please post a comment if any issues, I shall respond. If the answer helped, please do rate it. Thank you.

Note: The output shown in question above shows the word "Fares" 9th item in all tokens and 3rd in alphabet tokens. So I thought that word might be missing in the input file you gave and tested. Then the outputs matched. Please check the input file I have given below.

You need to give 2 command line arguments ( input and output filenames) to run the driver.

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<String> alphaTokens;
   private ArrayList<String> intTokens;
   private ArrayList<String> doubleTokens;
   private ArrayList<String> otherTokens;
   private ArrayList<String> 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<String>();
       alphaTokens = new ArrayList<String>();
       intTokens = new ArrayList<String>();
       doubleTokens = new ArrayList<String>();
       otherTokens = new ArrayList<String>();
       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 < token.length(); i++)
                       {
                           ch = token.charAt(i);
                           if(Character.isLetter(ch)) //is letter?
                           {
                               letters++;
                           }
                           else if(Character.isDigit(ch))
                           {
                               digits++;
                           }
                           else if(!Character.isWhitespace(ch))
                           {
                               otherchars++;
                           }
                       }
                      
                   }
              
           }
           lineScanner.close();
       }
       fileScanner.close();  
      
      
   }
   public void printStats() throws IOException
   {
       PrintWriter writer = new PrintWriter(new File(outFilename));
       printStats(writer);
       writer.close();
      
       //now show the output to console from the output file
       Scanner output = new Scanner(new File(outFilename));
       while(output.hasNextLine())
           System.out.println(output.nextLine());
       output.close();
      
   }
   private void printStats(PrintWriter writer) throws IOException
   {     
       writer.println("1." + " " + "All tokens" );
       printList(writer, allTokens);
       writer.println(" 2." + " " + "All Alphabet tokens");
       printList(writer, alphaTokens);
       writer.println(" 3." + " " + "Double tokens");
       printList(writer, doubleTokens);
       writer.println(" 4." + " " + "Integer tokens");
       printList(writer, intTokens);
       writer.println(" 5." + " " + "Other tokens");
       printList(writer, otherTokens);
       writer.println(" 6" + " " + "Counts, largest and longest" + " ");
       writer.println(" Count of non-empty lines: " + nonEmptyLines );
       writer.println(" Count of all tokens: " + allTokens.size());
       writer.println(" Count of alphabet tokens: " + alphaTokens.size());
       writer.println(" Count of Integer tokens: "+ intTokens.size());
       writer.println(" Count of Double tokens: " + doubleTokens.size());
       writer.println(" Count of Other tokens: " + otherTokens.size());
      
       writer.println(" Count of numberOfCharacters: " + (letters + digits +otherchars));
       writer.println(" Count of digits: " + digits);
       writer.println(" Count of letters: " + letters);
       writer.println(" Count of non-digit & letters: " + otherchars);
       writer.println(" Longest alphabet token: " + longestAlphaToken);
       writer.println(" Largest numeric value: "+ largestNumeric);
      
      
   }
  
   private void printList(PrintWriter writer,ArrayList list)
   {
       for(int i = 0; i < list.size(); i++)
       {
           writer.println(" " + (i+1) + " " + list.get(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());

       }

         

   }

}

input file

CS 1302 A

6 / 22 / 2017

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

Test1 89.5

Test2 100

Test3 55.75

Final 100.50.759

Quiz 25.

When are we going to have a test?

Are the PPT slides helpful?

You have done a good job!!!

output

1.   All tokens

       1   CS

       2   1302

       3   A

       4   6

       5   /

       6   22

       7   /

       8   2017

       9   Fares

       10   An

       11   Exception

       12   can

       13   be

       14   anything

       15   which

       16   interrupts

       17   the

       18   normal

       19   flow

       20   of

       21   the

       22   program.

       23   When

       24   an

       25   exception

       26   occurs

       27   program

       28   processing

       29   gets

       30   terminated

       31   and

       32   doesn’t

       33   continue

       34   further

       35   In

       36   such

       37   cases

       38   we

       39   get

       40   a

       41   system

       42   generated

       43   error

       44   message

       45   The

       46   good

       47   thing

       48   about

       49   exceptions

       50   is

       51   that

       52   they

       53   can

       54   be

       55   handled

       56   Test1

       57   89.5

       58   Test2

       59   100

       60   Test3

       61   55.75

       62   Final

       63   100.50.759

       64   Quiz

       65   25.

       66   When

       67   are

       68   we

       69   going

       70   to

       71   have

       72   a

       73   test?

       74   Are

       75   the

       76   PPT

       77   slides

       78   helpful?

       79   You

       80   have

       81   done

       82   a

       83   good

       84   job!!!

2.   All Alphabet tokens

       1   CS

       2   A

       3   Fares

       4   An

       5   Exception

       6   can

       7   be

       8   anything

       9   which

       10   interrupts

       11   the

       12   normal

       13   flow

       14   of

       15   the

       16   When

       17   an

       18   exception

       19   occurs

       20   program

       21   processing

       22   gets

       23   terminated

       24   and

       25   continue

       26   further

       27   In

       28   such

       29   cases

       30   we

       31   get

       32   a

       33   system

       34   generated

       35   error

       36   message

       37   The

       38   good

       39   thing

       40   about

       41   exceptions

       42   is

       43   that

       44   they

       45   can

       46   be

       47   handled

       48   Final

       49   Quiz

       50   When

       51   are

       52   we

       53   going

       54   to

       55   have

       56   a

       57   Are

       58   the

       59   PPT

       60   slides

       61   You

       62   have

       63   done

       64   a

       65   good

3.   Double tokens

       1   89.5

       2   55.75

4.   Integer tokens

       1   1302

       2   6

       3   22

       4   2017

       5   100

5.   Other tokens

       1   /

       2   /

       3   program.

       4   doesn’t

       5   Test1

       6   Test2

       7   Test3

       8   100.50.759

       9   25.

       10   test?

       11   helpful?

       12   job!!!

6   Counts, largest and longest

       Count of non-empty lines: 19

       Count of all tokens: 84

       Count of alphabet tokens: 65

       Count of Integer tokens: 5

       Count of Double tokens: 2

       Count of Other tokens: 12

       Count of numberOfCharacters: 376

       Count of digits: 34

       Count of letters: 328

       Count of non-digit & letters: 14

       Longest alphabet token: interrupts

       Largest numeric value: 2017.0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote