sorting members

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2007
Posts: 20
Reputation: pavani2006 is an unknown quantity at this point 
Solved Threads: 0
pavani2006 pavani2006 is offline Offline
Newbie Poster

sorting members

 
0
  #1
Apr 23rd, 2007

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
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,628
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: sorting members

 
0
  #2
Apr 23rd, 2007
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...

  1. import java.io.BufferedReader;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.util.Arrays;
  6. import java.util.Comparator;
  7. import java.util.TreeMap;
  8.  
  9. class CompareMappings implements Comparator
  10. {
  11. public int compare(Object arg0, Object arg1)
  12. {
  13. Mappings m1 = (Mappings)arg0;
  14. Mappings m2 = (Mappings)arg1;
  15. return(m1.getSmartness() - m2.getSmartness());
  16. }
  17. }
  18.  
  19. class Mappings
  20. {
  21. private String name;
  22. private int smartness;
  23.  
  24. Mappings(String rawStr)
  25. {
  26. rawStr = rawStr.trim();
  27. int index = rawStr.lastIndexOf(' ');
  28. setName(rawStr.substring(0, index + 1));
  29. setSmartness(rawStr.substring(index + 1));
  30. }
  31.  
  32. public String getName()
  33. {
  34. return name;
  35. }
  36.  
  37. public int getSmartness()
  38. {
  39. return smartness;
  40. }
  41.  
  42. void setName(String name)
  43. {
  44. this.name = name;
  45. }
  46.  
  47. void setSmartness(String smartness)
  48. {
  49. try
  50. {
  51. this.smartness = Integer.parseInt(smartness);
  52. }
  53. catch(Exception e)
  54. {
  55. System.out.println("AN exception occured while settings smartness...: ");
  56. }
  57. }
  58.  
  59. public String toString()
  60. {
  61. return("Name: " + name + " Rating: " + smartness);
  62. }
  63.  
  64. public boolean equals(Object arg0)
  65. {
  66. Mappings m = (Mappings)arg0;
  67. return name.equalsIgnoreCase(m.name);
  68. }
  69. }
  70.  
  71. public class Temp
  72. {
  73. public Object[] populateFromFile(String path)
  74. {
  75. TreeMap map = new TreeMap(); //use TreeMap to avoid duplicates
  76. try
  77. {
  78. String temp = null;
  79. BufferedReader br = new BufferedReader(new FileReader(path));
  80. while((temp = br.readLine()) != null)
  81. {
  82. Mappings m = new Mappings(temp);
  83. map.put(m.getName(), m);
  84. }
  85. }
  86. catch(FileNotFoundException e)
  87. {
  88. System.out.println("File not found....");
  89. }
  90. catch(IOException e)
  91. {
  92. System.out.println("An exception occured with the input stream while reading the file...");
  93. }
  94. return map.values().toArray();
  95. }
  96.  
  97. public static void main(String[] args)
  98. {
  99. Temp temp = new Temp();
  100. // avoid using absolute, hardcoded paths
  101. Object[] array = temp.populateFromFile("c:\\data.txt");
  102. Arrays.sort(array, new CompareMappings());
  103. for(int i = 0; i < array.length; ++i)
  104. System.out.println(array[i].toString());
  105. }
  106. }
Last edited by ~s.o.s~; Apr 23rd, 2007 at 2:57 pm.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: sorting members

 
0
  #3
Apr 23rd, 2007
Homework is given for a reason. It's one thing pointing someone in the right direction, another [attempting] to do the entire thing yourself.
Last edited by iamthwee; Apr 23rd, 2007 at 4:26 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,628
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: sorting members

 
0
  #4
Apr 23rd, 2007
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 20
Reputation: pavani2006 is an unknown quantity at this point 
Solved Threads: 0
pavani2006 pavani2006 is offline Offline
Newbie Poster

Re: sorting members

 
0
  #5
Apr 26th, 2007
Originally Posted by ~s.o.s~ View Post
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,628
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: sorting members

 
0
  #6
Apr 26th, 2007
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC