- 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 ArunRamesh SureshRamesh SatishSuresh 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
slution:
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=s;
System.out.println(a);i++;
}
for ( i=0;i<a.length; i++ )
{
//if(!a.equals(a[i+1]))
}
//System.out.println(str);
//fr.close();
}
}
catch ( Exception e)
{
System.out.println(e);
}
//System.out.println("Hello World!");
}
}
i coulnot sort them according to their smartness.how to sort them accoeding to their smartness to get that out put