Results 1 to 6 of 6

Thread: output

  1. #1
    yrollgayanth is offline Member
    Join Date
    Dec 2009
    Posts
    11
    Rep Power
    0

    Default output

    Hello every one. I'm having problem regarding the output of my program. This program read data from a text file & sort them out correspond to the lap time & the print them in ascending order.

    Original text file as follows

    1 334 John Steffensen AUS 44.82
    2 500 Takeshi Fujiwara ESA 46.92
    3 651 Dim�trios R�gas 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

    Output

    {44.5=4 352 Chris Brown BAH
    , 44.82=1 334 John Steffensen AUS
    , 44.92=6 491 Arismendy Peguero DOM
    , 45.44=8 626 Bastian Swillims GER
    , 45.7=5 1050 Rennyuow Santa TRI
    , 45.83=7 897 Marcin Marciniszyn POL
    , 46.22=3 651 Dim�trios R�gas GRE
    , 46.92=2 500 Takeshi Fujiwara ESA
    }



    Following is 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(){


    HashMap<Double,String> map = new HashMap<Double,String>();
    while(x.hasNext()){ //as long as data contain in file

    int lane = x.nextInt();
    int number = x.nextInt();
    String firstname = x.next();
    String surname = x.next();
    String country = x.next();
    double laptime = x.nextDouble();

    map.put(laptime,lane+"\t "+number+"\t "+firstname+"\t "+surname+"\t "+country+"\n");

    }
    Map<Double, String> sortedMap = new TreeMap<Double, String>(map);


    System.out.println(sortedMap+"\n");

    }


    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();
    }

    }

    I want this program to print only the first three results which is

    {44.5=4 352 Chris Brown BAH
    , 44.82=1 334 John Steffensen AUS
    , 44.92=6 491 Arismendy Peguero DOM

    Can someone help me to do that please?

    Thanx for your time.
    Last edited by yrollgayanth; 12-30-2009 at 03:41 PM.

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    This isn't advanced java topic.

    also, use code tags.

    to print only the first 3 things, instead of system out println the entire map, get a hold of the keyset from the sortedMap and print the first 3 of those, such as in a for loop.

  3. #3
    yrollgayanth is offline Member
    Join Date
    Dec 2009
    Posts
    11
    Rep Power
    0

    Default

    could you please show me the code??

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by travishein View Post
    This isn't advanced java topic.
    Agree. Thread moved.

    also, use code tags.
    Agree. OP, please see my signature below.

  5. #5
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    Java Code:
    //System.out.println(sortedMap+"\n"); 
    Iterator<Double> iterator = sortedMap.keySet().iterator();
    int shownCount = 0;
    while (iterator.hasNext() && shownCount < 3) {
      Double time = iterator.next();
      String name = sortedMap.get(time);
      System.out.println(time + ": " + name);
      shownCount ++;
    }

  6. #6
    yrollgayanth is offline Member
    Join Date
    Dec 2009
    Posts
    11
    Rep Power
    0

Similar Threads

  1. Can anyone tell me how this output?
    By vysh in forum Advanced Java
    Replies: 16
    Last Post: 05-10-2009, 08:32 PM
  2. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 11:44 PM
  3. different output
    By pro85 in forum Java Applets
    Replies: 6
    Last Post: 08-05-2008, 01:42 PM
  4. Why the output is always zero
    By mehrotra.chitij in forum New To Java
    Replies: 12
    Last Post: 04-25-2008, 04:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •