fork download
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Collections;
  4. import java.util.regex.Pattern;
  5.  
  6. import java.io.IOException;
  7. import java.io.FileInputStream;
  8. import java.io.InputStreamReader;
  9. import java.io.BufferedReader;
  10.  
  11. public class Main {
  12.  
  13. public static class Record {
  14. private int mLineNum;
  15. private String mWord;
  16. private double mValue;
  17.  
  18. public Record( int aLineNum, String aWord, double aValue ){
  19. this.mLineNum = aLineNum;
  20. this.mWord = aWord;
  21. this.mValue = aValue;
  22. }
  23.  
  24. public String getWord() { return mWord; }
  25. public int getLineNum() { return mLineNum; }
  26. public double getValue() { return mValue; }
  27.  
  28. @Override
  29. public String toString(){
  30. return this.mWord;
  31. }
  32. }
  33.  
  34. public static abstract class RecordSplitter {
  35. FileInputStream mFileInputStream;
  36. InputStreamReader mInputStreamReader;
  37. BufferedReader mBufferedReader;
  38. int mLineNumber;
  39.  
  40. public RecordSplitter( String filename, String encode )
  41. throws IOException
  42. {
  43. mLineNumber = 0;
  44. mFileInputStream = new FileInputStream(filename);
  45. mInputStreamReader = new InputStreamReader( mFileInputStream, encode );
  46. mBufferedReader = new BufferedReader(mInputStreamReader);
  47. }
  48.  
  49. public Record next() throws IOException {
  50. String line;
  51. Record record;
  52. do {
  53. line = mBufferedReader.readLine();
  54. record = this.createRecord(mLineNumber++,line);
  55. } while( line != null && record == null );
  56.  
  57. if( line == null ){
  58. mBufferedReader.close();
  59. mInputStreamReader.close();
  60. mFileInputStream.close();
  61. mLineNumber = -1;
  62. }
  63. return record;
  64. }
  65.  
  66. public abstract Record createRecord( int aLineNum, String aLine );
  67.  
  68. }
  69.  
  70. static public String DefaultFileName = "data.txt";
  71. static public String DefaultEncode = "JISAutoDetect";
  72.  
  73. public static void main( String aArgs[] ) throws IOException {
  74. RecordSplitter rs = new RecordSplitter(
  75. aArgs.length > 0 ? aArgs[0] : DefaultFileName,
  76. aArgs.length > 1 ? aArgs[1] : DefaultEncode
  77. ) {
  78. @Override
  79. public Record createRecord( int aLineNum, String aLine ){
  80. if( aLine == null || ! Pattern.matches("^.+\\s[0-9]+(\\.[0-9]+)?$",aLine) ){
  81. return null;
  82. }
  83. String[] kv = aLine.split("\\s");
  84. return new Record( aLineNum, kv[0], Double.parseDouble(kv[1]) );
  85. }
  86. };
  87.  
  88. List<Record> list = new ArrayList<Record>();
  89. for( Record r = rs.next(); r != null; r = rs.next() ){
  90. list.add(r);
  91. }
  92.  
  93. Collections.sort( list, new java.util.Comparator<Record>(){
  94. @Override
  95. public int compare( Record aObjLhs, Record aObjRhs ) {
  96. Record lhs = (Record) aObjLhs;
  97. Record rhs = (Record) aObjRhs;
  98. double sub = lhs.getValue() - rhs.getValue();
  99. if( sub == 0 ){ sub = lhs.getLineNum() - rhs.getLineNum(); }
  100. return -( sub < 0.0 ? -1 : (sub > 0.0 ? 1 : 0) );
  101. }
  102. });
  103.  
  104. System.out.println(list);
  105. }
  106. }
Runtime error #stdin #stdout 0.1s 212864KB
stdin
Standard input is empty
stdout
Standard output is empty