|
||
|
|||
|
Hello every1, im having a problem in Java, I wrote a program which can read data from a text file. It works fine so far (in terms of reading and giving the output). However i want this program to read data from the file and output in Ascending order.Can some one help me to do that? Following is the data of the text file & the code.
Original text file 25 Mark Fernando 34 John Cena 24 Vin Diesel I want above data as follows. 24 Vin Diesel 25 Mark Fernando 34 John Cena My code Java Code
import java.io.*;
import java.util.*;
public class Main {
public static class readfile{
private Scanner reader;
public void openfile(){
try{
reader = new Scanner (new File("Name.dat"));
}
catch (Exception e){
System.out.println("Could not locate the file");
}
}
public void readfile(){
while(reader.hasNext()){
String a = reader.next();
String b = reader.next();
String c = reader.next();
System.out.printf("%s %s %s\n",a,b,c);
}
}
public void closefile(){
reader.close();
}
}
public static void main(String[] args) {
readfile r = new readfile();
r.openfile();
r.readfile();
r.closefile();
}
}
Thanks
Last edited by Json; 27-12-2009 at 09:18 PM. Reason: Please use the code tags. |
|
|||
|
Thanks a lot for your reply. I will try that..
|
|
|||
|
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 Java 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();
}
}
Last edited by helloworld922; 31-12-2009 at 05:27 AM. |
|
||||
|
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. Java 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
}
}
__________________
ASCII a question .. Get an ANSI Please surround your code with [highlight=Java]code goes here[/highlight]. |
| The Following User Says Thank You to helloworld922 For This Useful Post: | ||
yroll (02-01-2010) | ||
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to Write and Read Binary Data | neo_2010 | Java Code Snippets and Tutorials | 3 | 04-01-2010 06:38 PM |
| How to Read the last line of a file | JavaPF | Java Code Snippets and Tutorials | 2 | 10-09-2009 07:26 AM |
| How to Read a Portion of a File | jazz2k8 | File I/O & Other I/O Streams | 3 | 07-07-2009 09:16 PM |
| exception while Read very large file > 300 MB | ps.ganesh | File I/O & Other I/O Streams | 2 | 12-06-2009 04:39 AM |
| text file | tyolu | File I/O & Other I/O Streams | 2 | 13-05-2009 12:17 PM |