Assignment No.8

  1. Write a method called measureSizes that accepts a File as its parameter, then reads that file twice, the first time to count the number of tokens in it and the second time to generate an array of String objects that are the tokens appearing in the File. For examples, if the File contains the tokens

    CSC120 Computer Programming I 2016 Fall
    Instructor Ogihara

    Then the array shall have eight elements

    “CSC120”, “Computer”, “Programmer”, “I”, “2016”, “Fall”, “Instructor”, “Ogihara”

  2. (Adapted from Textbook Chapter 6 Exercise 17) Write a method called inputStats that accepts a Scanner representing an input file and reports for each line its line position, the number of tokens in that line, and the length of the longest token in that line. To do this, consider reading each line of the file using the nextLine method, creating a Scanner from the input line, and accessing the tokens in that line using next and hasNext. For example, if the file had lines
    Beware the Jabberwock, my son,
    the hawas that bite, the claws that catch,
    Beware the JubJub bird and shun
    the frumious bandersnatch

    then the output of the program should be:

    Line 1 has 5 tokens (longest = 11)
    Line 2 has 8 tokens (longest = 6)
    Line 3 has 6 tokens (longest = 6)
    Line 4 has 3 tokens (longest = 13)

Go back to the assignment list page.