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 50 Viral Hits for 2021. The filename is global_charts.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(","); title[current]= vals[0]; rank[current]= vals[1]; Note that we will want to account for a possible FileNotFoundException by using a try-catch 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 = filterCol(value, 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.copyOfRange(matchingIdxs,0, 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 = filterCol(value, 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 =0; String firstDate = date[matchingIdxs[0]]; String lastDate = date[matchingIdxs[0]]; int[] trendCounts = new int[4]; for (int i =0; i < matchingIdxs.length; i++){ String currTitle = title[matchingIdxs[i]]; String currRank = rank[matchingIdxs[i]]; avgRank += Integer.parseInt(currRank); String currDate = date[matchingIdxs[i]]; if(currDate.compareTo(firstDate)<0){ firstDate = currDate; } if(currDate.compareTo(firstDate)>0){ lastDate = currDate; } String currArtist = artist[matchingIdxs[i]]; String currTrend = trend[matchingIdxs[i]]; if(currTrend.equals("MOVE_DOWN")){ trendCounts[0]= trendCounts[0]+1; } else if(currTrend.equals("SAME_POSITION")){ trendCounts[1]= trendCounts[1]+1; } else if(currTrend.equals("NEW_ENTRY")){ trendCounts[2]= trendCounts[2]+1; } else { trendCounts[3]= trendCounts[3]+1; } System.out.format("%-25s %8s %10s %8s %13s%n", currTitle, currRank, currDate, currArtist, currTrend); } avgRank /= matchingIdxs.length; System.out.println(); System.out.format("%-25s %13s%n", "Average Rank", avgRank); System.out.format("%-25s %13s%n", "First Date on Chart", firstDate); System.out.format("%-25s %13s%n", "Last Date on Chart", lastDate); int maxIdx =0; for(int i =1; i < trendCounts.length; i++){ if(trendCounts[maxIdx]< trendCounts[i]) maxIdx = i; } String trendName =""; if(maxIdx ==0){ trendName = "MOVE_DOWN"; } else if(maxIdx ==1){ trendName = "SAME_POSITION"; } else if(maxIdx ==2){ trendName = "SAME_POSITION"; } else { trendName = "MOVE_UP"; } System.out.format("%-25s %13s %8d%n", "Max Trend Count", trendName, trendCounts[maxIdx]); Sample Outputs: Example 1: Enter a data file to read: global_charts.csv E

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!