Welcome to Dream.In.Code
Become a Java Expert!

Join 149,478 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,524 people online right now. Registration is fast and FREE... Join Now!




reading fro a file

 
Reply to this topicStart new topic

reading fro a file, sorting persons

pavani2006
22 Apr, 2007 - 09:39 PM
Post #1

New D.I.C Head
*

Joined: 22 Feb, 2007
Posts: 15



Thanked: 2 times
My Contributions
1. Write a java program using following details. You are free to make all necessary assumptions. All the assumptions made should be documented.

There are four scientists who need to be ordered according to their smartness (smartest to least smart). There is a text file “smart.txt” in the following format:

Satish Arun
Ramesh Suresh
Ramesh Satish
Suresh Satish

Each line has a pair of nacmes, separated by an arbitrary number of white spaces (’ ’), where the person named by the first element of the pair is smarter than the person named by the second element of the pair. There is no limit to the number of such pairs listed in the file, but the listing would be sufficient to resolve the order of smartness of the four scientists.

Write a java program (ScientistResolver.java) that takes such a file and prints the list of all the distinct scientist names, without duplicates, one per line, ordered according to their smartness, as below.

Usage:
java ScientistResolver smart.txt

Result:
Ramesh
Suresh
Satish
Arun

the solution is:
CODE

import java.io.*;
import java.util.*;
class  ScientistResolver
{
    public static void main(String[] args) throws IOException
    {   //String arr[]=new String[100];
     //int s;
        try
        {
            FileReader fr=new FileReader("smart.txt");
            BufferedReader br = new BufferedReader(fr);
            String str;
            String a[]=new String[100];
int i=0;
            while((str=br.readLine())!=null)
            {
                StringTokenizer st=new StringTokenizer(str," ");
                while(st.hasMoreTokens())
                {
                    String s=st.nextToken();
                    
                     a[i]=s;
                    
System.out.println(a[i]);i++;
                }
            
                for ( i=0;i<a.length; i++ )
                {  
                   //if(!a[i].equals(a[i+1]))
                  
                }
                //System.out.println(str);
                //fr.close();
            }

        }
        catch ( Exception e)
        {
System.out.println(e);
        
        }

        //System.out.println("Hello World!");
    }
}


i could not print them and sort them accoeding to their smartness.please can any body help me.
User is offlineProfile CardPM
+Quote Post

ajwsurfer
RE: Reading Fro A File
23 Apr, 2007 - 10:04 AM
Post #2

D.I.C Regular
Group Icon

Joined: 24 Oct, 2006
Posts: 298



Thanked: 2 times
Dream Kudos: 50
My Contributions
OK, so you can print them out. Now see if you can store them in a class, one per class, and then print them out. After that you will be able to stort them. The class could look something like this (but could be more complex if you want it to be):
CODE

public class Professor {
  public string Fname;
  public string Lname;
  public int smartness;
  public string toString() { return Fname + ' ' Lname; }

}


Now you can say something like this:
CODE

new Professor p;
  p.Fname = s;
  p.Lname = s;
  p.smartness = i;
  System.out.println( p.toString() );

From there you will need to chose a data structure and way to store and sort them.

This post has been edited by ajwsurfer: 23 Apr, 2007 - 10:05 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 03:57PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month