Click here to Skip to main content
16,018,904 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the following code sorting order to be highest to lowest number.
Java
import java.io.*;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.regex.Pattern;

import javax.swing.text.html.HTMLDocument.Iterator;

public class new3 {
  public static void main(String arg[]) throws Exception {
    TreeMap tm = null;
    HashMap hm = new HashMap();
    String abc;
    String delimiter = ",";
    String lkeys = null;
    String lval = null;
    String j = "";
    String s6[] = new String[11];
    // BufferedReader br = null;
    Set s3;

    try {

      // System.out.println("htfhggh");
      String strLine;
      String s[] = new String[10];
      File f1 = new File("D:\\abc.txt");
      f1.createNewFile();
      FileWriter fw1 = new FileWriter(f1);
      PrintWriter pw = new PrintWriter(fw1);
      File f2 = new File("D:\\transtons1");

      // f2.mkdir();
      // System.out.println("htfhggh");
      s6 = f2.list();

      FileInputStream fib = null;
      BufferedReader br = null;
      // System.out.println(s6.length);
      for (int i = 0; i < s6.length; i++) {
        // System.out.println(":::::::::"+s6[i]);
        File f3 = new File("D:/transtons1/" + s6[i]);
        fib = new FileInputStream(f3);
        br = new BufferedReader(new InputStreamReader(fib));
        strLine = "";
        tm = new TreeMap();
        while ((strLine = br.readLine()) != null) {
          String s1 = "";
          String s2 = "";
          if ((strLine.equals("SUCCESS"))) {

            while ((strLine = br.readLine()) != null) {
              if (!(strLine.equals("SUCCESS"))) {

                // System.out.println("6");
                if (!(strLine.trim().equalsIgnoreCase(""))) {
                  // System.out.println("8");
                  // s1 = strLine.split(",")[1];
                  s2 = strLine.split(",")[0];

                  // System.out.println(":::::::::"+strLine);
                  if (strLine.split(",")[1].contains("-")) {
                    s1 = "0";
                  }
                  String[] sp = strLine.split(",");
                  if (sp.length == 2) {
                    // System.out.println("9");
                    double d = Double.parseDouble(sp[1]) - 6.192;
                    if (d < 0) {
                      // System.out.println("10");
                      sp[1] = "0";
                      if (sp[1].contains("E-4")) {
                        // System.out.println("11");
                        sp[1] = "0";
                      }
                    } else {
                      sp[1] = new Double(d).toString();
                    }
                  }
                  // System.out.println("htfhggh");
                  tm.put(s2, new String[] { sp[1], s6[i] });
                }
              } else {
                strLine = br.readLine();
              }
            }

          }
        }
        // printing here...
        Set set = tm.keySet();
        java.util.Iterator itTm = set.iterator();
        System.out.print(s6[i].substring(0, s6[i].indexOf(".log"))
            + ", ");
        pw.append(s6[i].substring(0, s6[i].indexOf(".log")) + ", ");
        String k = "";
        String k1 = "";
        while (itTm.hasNext()) {
          String firstString = (String) itTm.next();

          TreeMap tm1 = new TreeMap();

          // if only non-zero values are required then:
          /*
           * if(!((String[]) tm.get(firstString))[0].equals("0")){
           * System.out.print(firstString+",
           * "+((String[])tm.get(firstString))[0]+", ");
           * pw.append(firstString + ", " + ((String[])
           * tm.get(firstString))[0] + ", "); }
           */

          // if all values are required then:
          // System.out.print(((String[])tm.get(firstString))[0]+",
          // ");
          k = "";
          if (((String[]) tm.get(firstString))[0]
              .equalsIgnoreCase("0")) {
            k1 = k1 + firstString + ", "
                + ((String[]) tm.get(firstString))[0] + ", ";
            // k="";
            // System.out.print(" : "+ ((String[])
            // tm.get(firstString))[0]);
          } else {
            k = firstString + ", "
                + ((String[]) tm.get(firstString))[0] + ", ";
          }
          pw.append(k);
        }
        // pw.println("");
        pw.append(k1);
      }

      pw.flush();
      br.close();
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}


Current output is like this:
i/p file:-SearchRequest
TransactionInformation.TransactionControlNumber:	1371010101300003
TransactionInformation.Priority:	null
Data.Position:	1
Data.Position:	2
Data.Position:	3
Data.Position:	4
Data.Position:	5
Data.Position:	6
Data.Position:	7
Data.Position:	8
Data.Position:	9
Data.Position:	10
TargetGalleries:	
SUCCESS
3173055107830001,200.0288532
3327030712630003,1.8967291
1306031007490001,1.3557106

3404094507580005,2.0373015
3328087009450002,1.9978973
3325104612680001,1008.6304734


The output should be in the follownig order for me:
1371010101300003, 3173055107830001, 1002.83685319999998, 3325104612680001, 193.4384734, 1306031007490001, 0, 3327030712630003, 0, 3328087009450002, 0, 3404094507580005, 0,
Posted
Updated 9-Nov-11 6:05am
v3
Comments
Manfred Rudolf Bihy 9-Nov-11 8:07am    
Added code tags. Please always wrap your code in code tags!
Richard MacCutchan 9-Nov-11 8:33am    
Try making your code readable (8 spaces for a tab are far too many) and explain where it is going wrong.
Marc A. Brown 9-Nov-11 12:06pm    
I took care of the first part (fixing the indentation). Up to the OP to explain what's wrong. :)
Richard MacCutchan 9-Nov-11 14:00pm    
Well done, I was tempted but it looked like a bit too much work.
Marc A. Brown 9-Nov-11 14:05pm    
:) I cheated. Copied the code into Programmer's Notepad, replaced "\t" with " ", copied the result back into the CP question editing window.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900