944,062 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1791
  • Java RSS
Apr 23rd, 2007
0

sorting members

Expand Post »

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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pavani2006 is offline Offline
20 posts
since Feb 2007
Apr 23rd, 2007
0

Re: sorting members

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)
  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.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Apr 23rd, 2007
0

Re: sorting members

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 23rd, 2007
0

Re: sorting members

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.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Apr 26th, 2007
0

Re: sorting members

Click to Expand / Collapse  Quote originally posted by ~s.o.s~ ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pavani2006 is offline Offline
20 posts
since Feb 2007
Apr 26th, 2007
0

Re: sorting members

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.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Array problem
Next Thread in Java Forum Timeline: error in my code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC