| | |
sorting members
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2007
Posts: 20
Reputation:
Solved Threads: 0
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[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 coulnot sort them according to their smartness.how to sort them accoeding to their smartness to get that out put
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...
Java Syntax (Toggle Plain Text)
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()); } }
Last edited by ~s.o.s~; Apr 23rd, 2007 at 2:57 pm.
I don't accept change; I don't deserve to live.
•
•
Join Date: Feb 2007
Posts: 20
Reputation:
Solved Threads: 0
it is showing the exception "an exception occured while setting smartness"what to do
![]() |
Similar Threads
- To arrange Array Members in ascending order ! (Java)
- accessing private data members (C++)
- re: Sorting Algorithms (C++)
Other Threads in the Java Forum
- Previous Thread: Array problem
- Next Thread: error in my code
| Thread Tools | Search this Thread |
-xlint android api applet application array arrays automation bi binary blackberry block bluetooth chat class classes client code compile compiler component database developmenthelp draw eclipse error event exception fractal freeze game gameprogramming givemetehcodez graphics gui html ide image input integer j2me j2seprojects java javac javaprojects jetbrains jni jpanel jtable julia learningresources lego linux list login loop loops mac map method methods mobile netbeans newbie notdisplaying number online oracle page print problem program programming project qt recursion scanner screen server set singleton size sms sort sql string swing system template textfields threads time title tree tutorial-sample update variablebinding windows working xor






