java - Checking order 2d array issue -


i have xml page there data in 4 columns. these 4 columns need sorted, first on 1st column, on 2nd column , on. in first 2 columns there text, third column contains 3digit number , final column contains double or bigdecimal. take data xml , put in a 2d array check if ordering correct. in order test code i've written check ordering in columns i've created small 2d array: string [][] xml = new string [10][4];

after home 182 5.1

after home 182 5.9

after home 182 5.10

before away 150 4.6

before utah 852 3.8

before zion 110 110.99

before zion 110 110.100

control everywhere 475 65.1

control remote 369 38.6

control voip 598 44.9

this entire section of code used check ordering of columns (can copy pasted eclipse):

  string [][] xml = new string [10][4];    //column 1   xml[0][0]="after";   xml[1][0]="after";   xml[2][0]="after";   xml[3][0]="before";   xml[4][0]="before";   xml[5][0]="before";   xml[6][0]="before";   xml[7][0]="control";   xml[8][0]="control";   xml[9][0]="control";   //column 2   xml[0][1]="home";   xml[1][1]="home";   xml[2][1]="home";   xml[3][1]="away";   xml[4][1]="utah";   xml[5][1]="zion";   xml[6][1]="zion";   xml[7][1]="everywhere";   xml[8][1]="remote";   xml[9][1]="voip";   //column 3   xml[0][2]="182";   xml[1][2]="182";   xml[2][2]="182";   xml[3][2]="150";   xml[4][2]="852";   xml[5][2]="110";   xml[6][2]="110";   xml[7][2]="475";   xml[8][2]="369";   xml[9][2]="598";   //column 4   xml[0][3]="5.1";   xml[1][3]="5.9";   xml[2][3]="5.10";   xml[3][3]="4.6";   xml[4][3]="3.8";   xml[5][3]="110.99";   xml[6][3]="110.100";   xml[7][3]="65.1";   xml[8][3]="38.6";   xml[9][3]="44.9";    (int p=1;p<xml.length;p++){          int column1 = xml[(p)][0].compareto(xml[(p-1)][0]);         boolean sortcolumn1 = column1>=0; //if value 0 cells match (boolean true). if value equals 1 value in second cell  further in alphabet cell before it(boolean true). if value equals -1 value in second cell earlier in alphabet cell before it(boolean false).         if(sortcolumn1==false){//check on 1st column             system.out.println("1st colum error: " +xml[(p)][0]+ " before " +xml[(p-1)][0]);         }          //checking data in 2nd column should done when data in 2 adjacent cells of first column equal.             if (column1 == 0) {                 int column2 = xml[(p)][1].compareto(xml[(p - 1)][1]);                 if (column2 < 0) {// check on 2nd column                     system.out.println("column2 error: " + xml[(p)][1] + " before " + xml[(p - 1)][1]);                 }                 if (column2 == 0) {                     if (xml[(p - 1)][2] != "" && xml[(p)][2] != "") {                         try {                             integer = integer.parseint(xml[(p)][2]);                             integer b = integer.parseint(xml[(p - 1)][2]);                             integer column3 =  -b;                               if (column3 < 0) {// check on 3rd column                                 system.out.println("calculation column3: "+a+"-"+b+"=" + column3);                                 system.out.println("column 3 error: " + xml[(p)][2] + " lower " + xml[(p - 1)][2]);                             }                         } catch (numberformatexception e) {                         }                         int column3 = xml[(p)][2].compareto(xml[(p - 1)][2]);                         if (column3 == 0) {                             if (xml[(p - 1)][3] != "" && xml[(p)][3] != "") {                                 try {                                     bigdecimal = new bigdecimal(xml[(p)][3]);                                     bigdecimal b = new bigdecimal(xml[(p - 1)][3]);                                     bigdecimal column4 = a.subtract(b);                                       if (column4.compareto(bigdecimal.zero) < 0) {// check on 4th column                                         system.out.println("calculation column4: "  +a+"-"+b+"="+ column4);                                         system.out.println("column 4 error: " + xml[(p)][3] + " lower " + xml[(p - 1)][3]);                                     }                                 } catch (numberformatexception e) {                                 }                             }                         }                     }                 }             }         }     } 

the code seems work fine think 5.9 lower 5.10 , 110.99 lower 110.100 code disagrees. print result get:

calculation column4: 5.10-5.9=-0.80 column 4 error: 5.10 lower 5.9 calculation column4: 110.100-110.99=-0.890 column 4 error: 110.100 lower 110.99 

ive tried double instead of bigdecimal 5.10 shows 5.1. think problem can't figure out how solve it. guess possible splitting final column in 2 using . seperator , sorting 2 column bring it's own set of problems.

you can try use pojo instead of 2d array.

public class test implements comparable<test> {  private string field1;  private string field2;  private integer field3;  private double field4;  @override public int compareto(test o) {     // there sorting algorythm     return 0; } 

then after handle data in list of test class, u sort :

    final list<test> testlist = new arraylist<test>();     collections.sort(testlist); 

hope help.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -