Question: Create a program that reads in a CSV ( which stands for Comma Separated Values ) file and can search and filter the data in
Create a program that reads in a CSV which stands for Comma Separated Values file and can search and filter the data in the CSV file by column values. Background: We have created a CSV file that lists all of Spotifys Global Top Viral Hits for The filename is globalcharts.csv and it contains columns for title, rank, date, artist, and trend. The first method we have to write is loadFile. This method will first open the CSV file by creating a new File as fname and create a scanner to read the lines from the file. For each line that we read, we want to split that line on the comma and store the result in an array. We can then assign the values column data from the resulting split array to specific arrays representing the columns in the CSV file. This will keep track of the different data fields in the file to filter the data. For example: String line input.nextLine; String vals line.split; titlecurrent vals; rankcurrent vals; Note that we will want to account for a possible FileNotFoundException by using a trycatch statement in the loadFile method. Once you have finished writing the loadFile method, make sure to run your program to check that it can load the file before moving on to other methods. The next method that you will need to write is the filterCol method. We will need to construct a new array of integers to return from this method. Start by initializing this array to the length of the array that we are iterating over as the maximum number of matching indices is equal to the size of the array being passed This method takes a value and an array and checks to see if any of the elements in the array are equal to that value. Since we are comparing two Strings, you will want to compare the arrays element with value by using the equals method. If the two match, we want to store the index in our new array. int matchingIdxs filterColvalue arr; Once we have stored all of the matching indices, we will need to resize our array so that it does not include any default zero values. To resize, you will want to write: int matchingIdxsResize Arrays.copyOfRangematchingIdxs idx; Where matchingIdxs is our original array of matching indices, and idx is the last index where the given value matches. So this line copies our original array from its zeroth element to its element, which matches the value at the idxth position. After you have created this copy, return the matchingIdxsResize array from the filterCol method. The final method that you will need to write is the printFilterTable method. Your code should first call: int matchingIdxs filterColvalue arr; printHeader; Then, complete the method printFilterTable to initialize values for the statistical metrics we want to get from the indices listed in the matchingIdxs using the following code: float avgRank ; String firstDate datematchingIdxs; String lastDate datematchingIdxs; int trendCounts new int; for int i ; i matchingIdxs.length; i String currTitle titlematchingIdxsi; String currRank rankmatchingIdxsi; avgRank Integer.parseIntcurrRank; String currDate datematchingIdxsi; ifcurrDatecompareTofirstDate firstDate currDate; ifcurrDatecompareTofirstDate lastDate currDate; String currArtist artistmatchingIdxsi; String currTrend trendmatchingIdxsi; ifcurrTrendequalsMOVEDOWN" trendCounts trendCounts; else ifcurrTrendequalsSAMEPOSITION" trendCounts trendCounts; else ifcurrTrendequalsNEWENTRY" trendCounts trendCounts; else trendCounts trendCounts; System.out.formats s s s sn currTitle, currRank, currDate, currArtist, currTrend; avgRank matchingIdxs.length; System.out.println; System.out.formats sn "Average Rank", avgRank; System.out.formats sn "First Date on Chart", firstDate; System.out.formats sn "Last Date on Chart", lastDate; int maxIdx ; forint i ; i trendCounts.length; i iftrendCountsmaxIdx trendCountsi maxIdx i; String trendName ; ifmaxIdx trendName "MOVEDOWN"; else ifmaxIdx trendName "SAMEPOSITION"; else ifmaxIdx trendName "SAMEPOSITION"; else trendName "MOVEUP; System.out.formats s dn "Max Trend Count", trendName, trendCountsmaxIdx; Sample Outputs: Example : Enter a data file to read: globalcharts.csv E
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
