Skip to content
Advertisement

How to Sort Array and populate it

so I NEED to sort this array by column and populate it bu user input. so this is my code and if you can help me it would be great..

public class JavaApplication45 {
    
    public static void main(String[] args) throws IOException {
           
    String[][] AN = new String[8][5];

    AN[0][0] = "Curepipe";
    AN[0][1] = "-20.3162";
    AN[0][2] = "57.5166";
    AN[0][3] = "Plaine Wilhems";

    AN[1][0] = "Ebene";
    AN[1][1] = "-20.2456";
    AN[1][2] = ``"57.4842 ";
    AN[1][3] = "Plaine Wilhems ";

    AN[2][0] = "Rose Hill";
    AN[2][1] = "-20.2456";
    AN[2][2] = "57.4719";
    AN[2][3] = "Plaine Wilhems";

    AN[3][0] = "Beau Bassin";
    AN[3][1] = "-20.2276";
    AN[3][2] = "57.4681";
    AN[3][3] = "Plaine Wilhems";

    AN[4][0] = "Quatre Bornes";
    AN[4][1] = "-20.2654";
    AN[4][2] = "57.4778 ";
    AN[4][3] = "Plaine Wilhems";

    AN[5][0] = "Vacoas";
    AN[5][1] = "-20.2981";
    AN[5][2] = "57.4783";
    AN[5][3] = "Plaine Wilhems";

    AN[6][0] = "Mahebourg";
    AN[6][1] = "20.4081";
    AN[6][2] = "57.7000 ";
    AN[6][3] = "Grand Port    ";

    AN[7][0] = "Goodlands";
    AN[7][1] = "-20.0350";
    AN[7][2] = "57.6431";
    AN[7][3] = "Rivere du Rempart";


 int rows = 8;
    int columns = 4;
    int a, b;
    for (a = 0; a < rows; a++) {
        for (b = 0; b < columns; b++) {
            System.out.print(AN[a][b] + " t");

        }
        System.out.println(" ");

    }

so I NEED to sort this array by column and populate it bu user input. so this is my code and if you can help me it would be great..

Advertisement

Answer

As per your code i understand that you want print 5 column which is Postcode i implemented this column dynamically as per your 2nd 2D array values, check out my code and it’s output and confirm me is this answer useful to you or not

import java.util.*;

    public class JavaApplication45 {
    
    public static void main(String[] args) throws IOException {
           
    String[][] AN = new String[8][5];

    AN[0][0] = "Curepipe";
    AN[0][1] = "-20.3162";
    AN[0][2] = "57.5166";
    AN[0][3] = "Plaine Wilhems";

    AN[1][0] = "Ebene";
    AN[1][1] = "-20.2456";
    AN[1][2] = "57.4842 ";
    AN[1][3] = "Plaine Wilhems ";

    AN[2][0] = "Rose Hill";
    AN[2][1] = "-20.2456";
    AN[2][2] = "57.4719";
    AN[2][3] = "Plaine Wilhems";

    AN[3][0] = "Beau Bassin";
    AN[3][1] = "-20.2276";
    AN[3][2] = "57.4681";
    AN[3][3] = "Plaine Wilhems";

    AN[4][0] = "Quatre Bornes";
    AN[4][1] = "-20.2654";
    AN[4][2] = "57.4778 ";
    AN[4][3] = "Plaine Wilhems";

    AN[5][0] = "Vacoas";
    AN[5][1] = "-20.2981";
    AN[5][2] = "57.4783";
    AN[5][3] = "Plaine Wilhems";

    AN[6][0] = "Mahebourg";
    AN[6][1] = "20.4081";
    AN[6][2] = "57.7000 ";
    AN[6][3] = "Grand Port    ";

    AN[7][0] = "Goodlands";
    AN[7][1] = "-20.0350";
    AN[7][2] = "57.6431";
    AN[7][3] = "Rivere du Rempart";
    
    //zip table
    String[][] str1 = new String[8][2];

    str1[0][0] = "Curepipe";
    str1[0][1] = "74415";

    str1[1][0] = "Goodlands";
    str1[1][1] = "30401";

    str1[2][0] = "Rose Hill";
    str1[2][1] = "71360";

    str1[3][0] = "Beau Bassin";
    str1[3][1] = "71401";

    str1[4][0] = "Vacoas";
    str1[4][1] = "73304";

    str1[5][0] = "Mahebourg";
    str1[5][1] = "50810";

    str1[6][0] = "Quatre Bornes";
    str1[6][1] = "72249";

    str1[7][0] = "Ebene";
    str1[7][1] = "72201";


    System.out.println("City     tLat      tlong      tDistrict ttPostcode");
    System.out.println("-----------------------------------------------------------------------------------");
    int rows = 8;
    int columns = 5;
    int a, b, c;
    
    for (a = 0; a < rows; a++) {
        for (b = 0; b < columns; b++) {
            if(b<4){
                //use to formate values
                Formatter formatter = new Formatter();
                formatter.format( "%-7s",AN[a][b]);
                System.out.print(formatter + " t");
            }else{
                for(c=0;c<rows;c++){
                    if(AN[a][0]==str1[c][0]){
                        if(AN[a][3].length()>14){
                        //use to formate values
                        Formatter formatter = new Formatter();
                        formatter.format("%2s", str1[c][1]);
                        System.out.print(formatter + "  ");
                        }else{
                        //use to formate values
                        Formatter formatter = new Formatter();
                        formatter.format("%13s", str1[c][1]);
                        System.out.print(formatter + "  ");
                        }
                        
                         
                    }
                }
            }
            
        }
      
        System.out.println(" ");
        
    }
  }
     

}

Output : enter image description here

Advertisement