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

Edited 3 Years Ago by pyTony: fixed formatting

The problem is not as simple as it looks to be on the surface. Too many things to be taken care of -- handling duplicates, sorting the ratings, associating the ratings with the names. You would have to come up with a better implementation than string arrays. Here is my humble stab at it...

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.TreeMap;

class CompareMappings implements Comparator
{
    public int compare(Object arg0, Object arg1)
    {
        Mappings m1 = (Mappings)arg0;
        Mappings m2 = (Mappings)arg1;            
        return(m1.getSmartness() - m2.getSmartness());
    }  
}

class Mappings
{
    private String name;
    private int smartness;
    
    Mappings(String rawStr)
    {
        rawStr = rawStr.trim();
        int index = rawStr.lastIndexOf(' ');
        setName(rawStr.substring(0, index + 1));
        setSmartness(rawStr.substring(index + 1));
    }
    
    public String getName()
    {
        return name;
    }
    
    public int getSmartness()
    {
        return smartness;
    }
    
    void setName(String name)
    {
        this.name = name;
    }
    
    void setSmartness(String smartness)
    {
        try
        {
            this.smartness = Integer.parseInt(smartness);
        }
        catch(Exception e)
        {
            System.out.println("AN exception occured while settings smartness...: ");
        }
    }
    
    public String toString()
    {
        return("Name: " + name + "  Rating: " + smartness);
    }
    
    public boolean equals(Object arg0)
    {
        Mappings m = (Mappings)arg0;
        return name.equalsIgnoreCase(m.name);
    }
}

public class Temp
{
    public Object[] populateFromFile(String path)
    {
        TreeMap map = new TreeMap();  //use TreeMap to avoid duplicates
        try
        {
            String temp = null;
            BufferedReader br = new BufferedReader(new FileReader(path));
            while((temp = br.readLine()) != null)
            {
                Mappings m = new Mappings(temp);
                map.put(m.getName(), m);
            }
        }
        catch(FileNotFoundException e)
        {
            System.out.println("File not found....");
        }
        catch(IOException e)
        {
            System.out.println("An exception occured with the input stream while reading the file...");
        }
        return map.values().toArray();
    }
    
    public static void main(String[] args)
    {
        Temp temp = new Temp();
        // avoid using absolute, hardcoded paths
        Object[] array = temp.populateFromFile("c:\\data.txt");
        Arrays.sort(array, new CompareMappings());
        for(int i = 0; i < array.length; ++i)
            System.out.println(array[i].toString());
    }
}

Homework is given for a reason. It's one thing pointing someone in the right direction, another [attempting] to do the entire thing yourself.

It was just a pointer in the right direction. If the above question is really a homework and the solution is submitted as it is, the OP would have a good amount of explaining to do.

It was just a pointer in the right direction. If the above question is really a homework and the solution is submitted as it is, the OP would have a good amount of explaining to do.

it is showing the exception "an exception occured while setting smartness"what to do

Mine was a simple eg. which worked only when the names are seperated by spaces and not tabs. BTW, it works for me. Modify the program according to your needs.

This article has been dead for over six months. Start a new discussion instead.