Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Read data from text file

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Read data from text file

    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

    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; December 27th, 2009 at 05:18 PM. Reason: Please use the code tags.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default 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

  3. #3
    Junior Member
    Join Date
    Dec 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Read data from text file

    Quote Originally Posted by Json View Post
    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..

  4. #4
    Junior Member
    Join Date
    Dec 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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
    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?
    Last edited by helloworld922; December 31st, 2009 at 01:27 AM.

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

    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.

  6. The Following User Says Thank You to helloworld922 For This Useful Post:

    yroll (January 2nd, 2010)

Similar Threads

  1. How to Write and Read Binary Data
    By neo_2010 in forum File Input/Output Tutorials
    Replies: 3
    Last Post: January 4th, 2010, 02:38 PM
  2. Java program to read last line of a file
    By JavaPF in forum File Input/Output Tutorials
    Replies: 2
    Last Post: September 10th, 2009, 02:26 AM
  3. How to Read a Portion of a File in Java?
    By jazz2k8 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: July 7th, 2009, 04:16 PM
  4. exception while Read very large file > 300 MB
    By ps.ganesh in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: June 11th, 2009, 11:39 PM
  5. Java program to reduce spaces between the words in a text file
    By tyolu in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: May 13th, 2009, 07:17 AM