In: Computer Science
Leave code as is ALL I NEED IS TO MAKE THE LIST INTO ASCENDING ORDER FROM A TO Z OF ARTISTS NAMES
YOU CAN USE THIS AS REFERENCE SPOTIFY LIST TOP 50 AS REFERENCE FOR CSV FILE https://spotifycharts.com/viral/
import java.io.*; public class Spotify { public static void main(String[]args) { // Read csv Spotify file located on my desktop // Download csv file, save on desktop, and add file location in csvFile = " " String csvFile = "CSVFILE LOCATION OF TOP ARTISTS HERE"; String line = ""; // Try catch block when reading and opening files try { BufferedReader br = new BufferedReader(new FileReader(csvFile)); // Create text file File theFile = new File("Artists-Week-Of-09-01-2020.txt"); PrintStream out = new PrintStream(new FileOutputStream(theFile)); // While next line is not null parse the line with commas and store it into // values array while ((line = br.readLine()) != null) { String[] values = line.split(","); // Print artists name located in [2] to console System.out.println(values[2]); // Print artists name to text file report out.print(values[2] + "\n"); } // Close PrintStream out.close(); } catch(FileNotFoundException e){ e.printStackTrace(); } catch(IOException e){ e.printStackTrace(); } } }
In the spotify class declaraed the list variable .In the list variable adding the all the artist names and sorting that using the Collection.sort method.it will sort the values into the ascending order.Please find the below code to print the ascending order of the artistname.
In the code for the csvFile variable include the path of your csv file.
Please find the below code.
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Spotify {
public static void main(String[] args) {
// Read csv Spotify file located
on my desktop
// Download csv file, save on
desktop, and add file location in csvFile = " "
String csvFile = "YOUR CSV FILE
LOCATION";
String line = "";
// created one list to add the all
the artist name's of csv file
List<String> artistList = new
ArrayList<String>();
// Try catch block when reading and
opening files
try {
BufferedReader
br = new BufferedReader(new FileReader(csvFile));
// Create text
file
File theFile =
new File("Artists-Week-Of-09-01-2020.txt");
PrintStream out
= new PrintStream(new FileOutputStream(theFile));
// While next
line is not null parse the line with commas and store it into
// values
array
while ((line =
br.readLine()) != null) {
String[] values = line.split(",");
// Print artists name located in [2] to
console
// adding artist name into the list
// trim method to remove the any white spaces in
the starting or ending of the
// name.
artistList.add(values[2].trim());
}
// adding
Collections.sort it will do ascending order of the elements
Collections.sort(artistList);
// iterating
over the list
for (String
artistName : artistList) {
// printing sorted artist names into the
file
out.print(artistName + "\n");
// printing into the console
System.out.println(artistName);
}
// Close
PrintStream
out.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
input :
Below one is the input screenshort of csv file.
Output:
This is the artistnames with ascending order.
Summary:
Added on list variable included all the artistnames then sorted using the Collections.sort method.After sorting the names then printed into the file.