Re: Read data from text file
What you can do is read each line and put them in a TreeSet<String> and it will automatically sort it using natural order.
// Json
Re: Read data from text file
Quote:
Originally Posted by
Json
What you can do is read each line and put them in a TreeSet<String> and it will automatically sort it using natural order.
// Json
Thanks a lot for your reply. I will try that..
Sort data reads from a text file
Hello every1. I'm having a problem sorting data which reads from a text file.
Data in the text file as follows
1 334 John Steffensen AUS 44.82
2 500 Takeshi Fujiwara ESA 46.92
3 651 Dimitrios Rogas GRE 46.22
4 352 Chris Brown BAH 44.5
5 1050 Rennyuow Santa TRI 45.7
6 491 Arismendy Peguero DOM 44.92
7 897 Marcin Marciniszyn POL 45.83
8 626 Bastian Swillims GER 45.44
When program runs i want data to be sorted out in ascending order correspond to the lap time as follows.
4 352 Chris Brown BAH 44.5
1 334 John Steffensen AUS 44.82
6 491 Arismendy Peguero DOM 44.92
8 626 Bastian Swillims GER 45.44
5 1050 Rennyuow Santa TRI 45.7
7 897 Marcin Marciniszyn POL 45.83
3 651 Dimitrios Rogas GRE 46.22
2 500 Takeshi Fujiwara ESA 46.92
My code so far
Code :
import java.io.*;
import java.util.*;
public class Main {
public static class readfile{
private Scanner x;
public void openfile()throws java.io.IOException{
try{
FileReader fr = new FileReader("Lap.dat");
x = new Scanner(fr);
}
catch(Exception e){
System.out.println("Could not locate the file"+e);
}
}
public void readfile(){
int lane ;
int number ;
String firstname ;
String surname ;
String country ;
double laptime ;
while(x.hasNext()){ //as long as data contain in file
lane = x.nextInt();
number = x.nextInt();
firstname = x.next();
surname = x.next();
country = x.next();
laptime = x.nextDouble();
System.out.printf("%s %s %s %s %s %s\n",lane,number,firstname,surname,country,laptim e);
}
}
public void closefile(){
x.close();
}
}
public static void main(String[] args)throws java.io.IOException {
readfile r = new readfile();
r.openfile();
r.readfile();
r.closefile();
}
}
Can someone help me to do that please?
Re: Read data from text file
First of all, you'll need to decide what kind of sort algorithm you want to use, and which attribute you want to sort by. Next, you'll need to actually create some sort of data structure to hold all the data. Lastly you'll need to sort the data.
The simplest sorting algorithm worth bothering with is probably insertion sort. Wikipedia has a wonderful description about the algorithm here (there's even a little diagram that shows how the sorting process works).
I would strongly suggest creating a class that can hold all the data you want to read in. This will make sorting a lot easier.
Code :
public class Item{
int lane ;
int number ;
String firstname ;
String surname ;
String country ;
double laptime ;
public Item(int lane, int number, String firstName, String surName, String country, double laptime)
{
this.lane = lane;
this.number = number;
this.firstname = firstName;
this.surname = surName;
this.country = country;
this.laptime = laptime;
}
public int compareTo(Item otherItem)
{
// here's where you determine the order different items are with relation to each other. NOTE: this is not where to put your sorting algorithm! It only compares the relation of two individual items
}
}
I'll leave the implementation of the sorting algorithm up to you, as well as finishing the compareTo method. As if you need further help.