- Joined
- May 6, 2021
- Messages
- 4
- Reaction score
- 0
0
I have a task in which im given a CSV file which is like Name,Marks,Time next line Abe,24,23.6 etc with Strings and ints and i need to sort the Names based on Marks with merge sort and print them in another txt file.I have tried many ways but i think that it need to be done using an Arraylist with objects that as i read the file will be made and each will have a String and an Int,however i cant find the way to merge sort them later when needed.
Here is where i am now with all the info i found by other guys too The main class
'''
''' and 2 classes Metoxh and marksCompare which will be used to pass arguments to the objects of every line and pass them into they Arraylist(with Metoxh items). '''
Any help is much much appreciated
I have a task in which im given a CSV file which is like Name,Marks,Time next line Abe,24,23.6 etc with Strings and ints and i need to sort the Names based on Marks with merge sort and print them in another txt file.I have tried many ways but i think that it need to be done using an Arraylist with objects that as i read the file will be made and each will have a String and an Int,however i cant find the way to merge sort them later when needed.
Here is where i am now with all the info i found by other guys too The main class
'''
Java:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
public class Sort
{
public static void main(String[] args)throws IOException
{
//Creating BufferedReader object to read the input text file
BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\user\\Desktop\\ale.us.txt"));
//Creating ArrayList to hold Student objects
ArrayList<Metoxh> studentRecords = new ArrayList<Metoxh>();
//Reading Student records one by one
String currentLine = reader.readLine();
try {
while (currentLine != null)
{
String[] studentDetail = currentLine.split("");
String name = studentDetail[0];
int marks = Integer.valueOf(studentDetail[1]);
//Creating Student object for every student record and adding it to ArrayList
studentRecords.add(new Metoxh(name, marks));
currentLine = reader.readLine();
}
}catch(NumberFormatException e) {}
//Sorting ArrayList studentRecords based on marks
Collections.sort(studentRecords, new marksCompare());
//Creating BufferedWriter object to write into output text file
BufferedWriter writer = new BufferedWriter(new FileWriter("C:\\Users\\user\\Desktop\\skr.txt"));
//Writing every studentRecords into output text file
for (Metoxh student : studentRecords)
{
writer.write(student.name);
writer.write(" "+student.marks);
writer.newLine();
}
//Closing the resources
reader.close();
writer.close();
}
}
''' and 2 classes Metoxh and marksCompare which will be used to pass arguments to the objects of every line and pass them into they Arraylist(with Metoxh items). '''
Java:
public class Metoxh {
String name;
int marks;
public Metoxh(String name, int marks)
{
this.name = name;
this.marks = marks;
}
}
import java.util.Comparator;
class nameCompare implements Comparator<Metoxh>
{
@Override
public int compare(Metoxh s1, Metoxh s2)
{
return s1.name.compareTo(s2.name);
}
}
Any help is much much appreciated