Hiii All :D

How are you doing ? i'm such a beginner in Java ! however , i made this code

i don't know how much professional is it but , I need to use Jfream when I deal with user and Jframe to print wherever I'm using JTable ! and yah i know Jfram needs 2D Aarrays and i made them :)

here is the code ::

public class District { // class

    private String DistrictName; // variable with type string for the districtName
    private Region[] regions; // because each district is divided to Regions
    // an array of regions is here :)

    public District() { // zero argument constructor
        this.DistrictName = null;
    }

    public District(String name, Region[] regions) { // constructor wih TWO argument as needed when i creat the functions :)
        this.DistrictName = name;
        this.regions = regions;
    }

    public District(String name) { // constructore with One argument
        this.DistrictName = name;
    }
// set the districtname
    public void setDistrictName(String districtName) {
        this.DistrictName = districtName;
    }
// get the districtname
    public String getDistrictName() {
        return DistrictName;
    }
// get an Array , the array of regions "POINTER TO THE ARRAY !"
    public Region[] getRegions() {
        return regions;
    }
}
public class Region {
// Variables

    private String RegionName;
    private String district;
    private SchoolGroup[] schools; // bcoz each Region has many schools , an array of schools is here :)

    public Region() {// zero argument constructor
    }

    public Region(String s, SchoolGroup[] school) {// constructor with 2 arguments
        this.RegionName = s;
        this.schools = school;

    }

    Region(String RegionName) { // constructor with ONE argument
        this.RegionName = RegionName;
    }
//set the distric

    public void setDistrict(String district) {
        this.district = district;
    }
// set the region name

    public void setName(String name) {
        this.RegionName = name;
    }
// get the region

    public String getRegionName() {
        return RegionName;
    }
// get POINTER to the schools array 

    public SchoolGroup[] getSchoolGroup() {
        return schools;
    }
}
public class SchoolGroup {

// variables ...
    protected int adminEmpNum;
    protected int teachersNum;
    protected int studentsNum;
    protected int numOfSchools;
    protected String schoolType;
// zero argument constructore

    public SchoolGroup() {
        this.adminEmpNum = 0;
        this.numOfSchools = 0;
        this.studentsNum = 0;
        this.teachersNum = 0;
        this.schoolType = null;

    }
// constrcture with argument

    public SchoolGroup(String schoolType, int numOfSchool, int adminEmpNum, int teachersNum, int studentsNum) {
        this.schoolType = schoolType;
        this.numOfSchools = numOfSchool;
        this.teachersNum = teachersNum;
        this.studentsNum = studentsNum;
        this.adminEmpNum = adminEmpNum;
    }
// set all the variables

    public void setInfo(int numOfSchool, int adminEmpNum, int teachersNum, int studentsNum) {
        this.teachersNum = teachersNum;
        this.numOfSchools = numOfSchool;
        this.adminEmpNum = adminEmpNum;
        this.studentsNum = studentsNum;
    }

    // get functions 
    public int getAdminEmpNum() {
        return adminEmpNum;
    }

    public int getTeachersNum() {
        return teachersNum;
    }

    public int getStudentsNum() {

        return studentsNum;

    }

    public int getNumOfSchools() {
        return numOfSchools;
    }

    public String getSchoolType() {
        return schoolType;
    }

    public int getInfo() {

        return 0;
    }
}
import java.util.*; // this import used for Scanner and exceptions :)
import javax.swing.*;

public class Main { // the main class

    // an array with type District .. Contain all KSA district except Makkah :)
    public static District DistrictList[] = {null
            , new District("Maddinah")
            , new District("Al Riyadh")
            , new District("Al Bahah")
            , new District("Jizan")
            , new District("Najran")
            , new District("Tabuk")
            , new District("Eastern Province")
            , new District("Asir")
            , new District("Ha'il")
            , new District("Al Qasim")
            , new District("Al Jawf")
            , new District("Northern Border")};
    public static Region regions[]; // array of type regions
//_________________________________________________________________
    public static void main(String[] args) {
        // Define schools information
        // each array for ONE region , each region has three kind of schools
        // each school has number of building ,admin , teachers and students
        SchoolGroup[] JeddahSchool = {new SchoolGroup("PrimarySchool", 100, 500, 1000, 40000),
            new SchoolGroup("IntermediateSchool", 87, 700, 3000, 35000),
            new SchoolGroup("HighSchool", 78, 7000, 5000, 33000)};

        SchoolGroup[] TaifSchool = {new SchoolGroup("PrimarySchool", 50, 100, 400, 13000),
            new SchoolGroup("IntermediateSchool", 45, 200, 2000, 15000),
            new SchoolGroup("HighSchool", 32, 400, 3000, 13000)};

        SchoolGroup[] makkahSchool = {new SchoolGroup("PrimarySchool", 50, 100, 400, 13000),
            new SchoolGroup("IntermediateSchool", 35, 80, 400, 12000),
            new SchoolGroup("HighSchool", 30, 90, 450, 1000)};


        //Define Makkah Regions regions
        Region jeddah = new Region("Jeddah", JeddahSchool);
        Region makkah = new Region("Makkah", makkahSchool);
        Region taif = new Region("Taif", TaifSchool);
        regions = new Region[3];
        regions[0] = jeddah;
        regions[1] = makkah;
        regions[2] = taif;


        //define The main District "Makkah" which is the only district we are
        // going to use it n this program
        District MakkahDistrict = new District("Makkah", regions); // send the regions to Makkah District as we need in the methods
        DistrictList[0] = MakkahDistrict; //initialise the first place n MakkahDistrict with both makkah district and it regions


        Scanner input = new Scanner(System.in);
         int answer = 0;
        String regionAns;
        String DistrictAns;

        while (answer != 11) {// to exite ..
            try
            {
            System.out.println(); // just to make it more arrangeable
            System.out.println();
                 // list to choose from .. containe everything the program can do :)
            System.out.println("1. the number of schools in that region, according to District Name.");
            System.out.println("2. the number of schools in that region, according to Region Name.");
            System.out.println("3. the number of schools in secondary or intermediate or high schools to the district");
            System.out.println("4. the number of schools in secondary or intermediate or high schools to the district, region");
            System.out.println("5. the total number of schools in District.");
            System.out.println("6. the total number of schools in region");
            System.out.println("7. the number of admin employees, teachers and students, according to District Name.");
            System.out.println("8. the number of admin employees or teachers or students in district");
            System.out.println("9. the number of admin employees or teachers or students in region");
            System.out.println("10.the number of admin employees, teachers and students, according to district, region, school level");
            System.out.println("11. Exit");
            System.out.println("What Do you want to do ,enter the number ");
            answer = input.nextInt();
            } catch (InputMismatchException e) { // when the user input a string instead of intger
                System.err.print("" + e + ", plz insert a number from the list above ");
            }

                switch (answer) {
// each case has just a call to da method which can do whatever in the list
                    case 1:
                        NumOfSchoolInRegions();
                        break;
                    case 2:
                        NumberOfOneTypeOfSchoolsInOneRgion();
                        break;
                    case 3:
                        NumberOfSpecificTypeOfSchoolsInDistrcit(); // here
                        break;
                    case 4:
                        NumberOfSchoolsInSpecificLevel();
                        break;
                    case 5:
                        NumberOfSchoolsInDistrict();
                        break;
                    case 6:
                        NumberOfSchoolsInRegion(); 
                        break;
                    case 7:
                        NumberOfEmployeeAndTeacherAndStudentInRegion();
                        break;
                    case 8:
                        NumberOfEmployeeAndTeacherAndStudentInDistrict();
                        break;
                    case 9:
                        NumberOfOneKindOFpplInRegion(); 
                        break;
                    case 10:
                        NumberOfPPLInOneSchoolLevelInRegion(); 
                        break;
                    case 11:
                        System.out.print("Bye Bye");
                    default:
                       
                }

//            } catch (InputMismatchException e) { // when the user input a string instead of intger
//                System.err.print("" + e + ", plz insert a number from the list above ");
//            }


        }
    }

    // Query 1.	Given the district, list the number of schools in that region, according to:
    public static void NumOfSchoolInRegions() {
     District district =getUserDistrict();

        Region[] regions = district.getRegions();
       
        String[][] toprint = new String[4][4];
        toprint[0][0] = district.getDistrictName();
        toprint[1][0] = "Primary School";
        toprint[2][0] = "Middle School";
        toprint[3][0] = "High School";
        for (int i = 1; i <= regions.length; i++) {
            SchoolGroup[] s = regions[i - 1].getSchoolGroup();
            toprint[0][i] = regions[i - 1].getRegionName();
            //convert int to string by calling the static method toString()
            // sourse : http://www.javadb.com/convert-an-int-value-to-string

            toprint[1][i] = Integer.toString(s[0].getNumOfSchools()); 
            toprint[2][i] = Integer.toString(s[1].getNumOfSchools());
            toprint[3][i] = Integer.toString(s[2].getNumOfSchools());
        }

         PrintToprintArray(toprint, 4, 4);
    }

    //--------------------------------------------------------------------
    // Query 2.Given the district, region, list the number of schools in that region
    public static void NumberOfOneTypeOfSchoolsInOneRgion() {
        District district =getUserDistrict();

        Region regions = getUserRegion();


        SchoolGroup[] sg = regions.getSchoolGroup();
        // making 2D array
        String[][] toprint = new String[2][4];
        toprint[0][0] = regions.getRegionName();
        toprint[0][1] = "Primary School";
        toprint[0][2] = "Middle School";
        toprint[0][3] = "High School";
        toprint[1][0] = "# of schools";
        toprint[1][1] = Integer.toString(sg[0].getNumOfSchools());
        toprint[1][2] = Integer.toString(sg[1].getNumOfSchools());
        toprint[1][3] = Integer.toString(sg[2].getNumOfSchools());

         PrintToprintArray(toprint, 2, 4);
        /*
         
         */
//        for (int i = 0; i < 2; i++) {
//            for (int j = 0; j < 4; j++) {
//                System.out.print(toprint[i][j] + "           |    ");
//            }
//            System.out.println();
//
//        }
    }
      // Query 3.Given the district, list the number of schools in secondary or intermediate or high schools.

    public static void NumberOfSpecificTypeOfSchoolsInDistrcit() {
       District district =getUserDistrict();
        Region[] regions = district.getRegions();
Scanner input = new Scanner (System.in);
        String[][] toprint = new String[2][4];

        System.out.println("Choose School Level ");
        System.out.println("1- Primary School");
        System.out.println("2- Intermediate School");
        System.out.println("3- High School ");
        System.out.print("Your choice:  ");
         int answer = input.nextInt();

        if (answer == 1) {
            toprint[1][0] = "Primary School";
        } 
        else if (answer == 2) {
            toprint[1][0] = "Intermediate School";
        } 
        else if (answer == 3) {
            toprint[1][0] = "High School";
        }
        

        toprint[0][0] = district.getDistrictName();
        for (int i = 1; i <= regions.length; i++) {
            SchoolGroup[] s = regions[i - 1].getSchoolGroup();
            toprint[0][i] = regions[i - 1].getRegionName();
            if (answer == 1) {
                toprint[1][i] = Integer.toString(s[0].getNumOfSchools());
            } else if (answer == 2) {
                toprint[1][i] = Integer.toString(s[1].getNumOfSchools());
            } else if (answer == 3) {
                toprint[1][i] = Integer.toString(s[2].getNumOfSchools());
            }

        }
        // print the toprint array
        PrintToprintArray(toprint, 2, 4);
//        for (int i = 0; i < 2; i++) {
//            for (int j = 0; j < 4; j++) {
//                System.out.print(toprint[i][j] + "           |    ");
//            }
//            System.out.println();
//
//        }
    }

 //Query 4. Given the district, region, list the number of schools in secondary or intermediate or high schools.

    public static void NumberOfSchoolsInSpecificLevel() {
Scanner input = new Scanner (System.in);

     District district =getUserDistrict();

        Region[] regions = district.getRegions();

        for (int i = 0; i < regions.length; i++) {
            System.out.println((i + 1) + "- " + regions[i].getRegionName());
        }
        // scanner
      int  answer = input.nextInt();
        answer--;
        SchoolGroup[] sg = regions[answer].getSchoolGroup();

             System.out.println("1.Primary School");
             System.out.println("2.Intermediate School");
             System.out.println("3.High School");
             System.out.println("Choose the School Level :");
             answer = input.nextInt();

           System.out.println("the numbere of schools is :"+sg[answer-1].getNumOfSchools());
//           String[][] toprint = new String[2][2];
//           toprint[0][1]= " Number Of Schools ";
//
//
//       for (int i = 0; i < 2; i++) {
//            for (int j = 0; j < 2; j++) {
//                System.out.print(toprint[i][j] + "           |    ");
//            }
//            System.out.println();
//        }

    }


    //Query 5.Given the district, display the total numberNumberOfSchoolsInDistrict of schools.
    public static void NumberOfSchoolsInDistrict() {

        int totalNumberOfSchoolsInDstrict = 0;
        District district = getUserDistrict();

        for (int i = 0; i < regions.length; i++) {

            SchoolGroup schools[] = regions[i].getSchoolGroup();

            for (int j = 0; j < schools.length; j++) {
                totalNumberOfSchoolsInDstrict = totalNumberOfSchoolsInDstrict + schools[j].numOfSchools;

            }
        }

        System.out.println("Total Number of Schools " + totalNumberOfSchoolsInDstrict);
    }

    //Query 6.Given the district, region, display the total number of schools
   
    public static void NumberOfSchoolsInRegion() {
        int totalNumOfSchoolsNRrgion = 0;
       District district =getUserDistrict();

       Region regions = getUserRegion();
        
        SchoolGroup s[] = regions.getSchoolGroup();
             
        for (int j = 0; j < s.length; j++) {
            totalNumOfSchoolsNRrgion = totalNumOfSchoolsNRrgion + s[j].numOfSchools;

        }        
        System.out.println("Total Number of Schools " + totalNumOfSchoolsNRrgion);
    }

    //Query 7.Given the district, list the number of admin employees, teachers and students, according to:
    public static void NumberOfEmployeeAndTeacherAndStudentInRegion() {
       District district =getUserDistrict();

        Region[] regions = district.getRegions();
       
        String[][] toprint = new String[4][4];
        toprint[0][0] = "Primary Schools";
        toprint[1][0] = "Admins";
        toprint[2][0] = "Techers";
        toprint[3][0] = "Students";

        for (int i = 1; i <= regions.length; i++) {
            SchoolGroup[] sg = regions[i - 1].getSchoolGroup();
            toprint[0][i] = regions[i - 1].getRegionName();
            toprint[1][i] = Integer.toString(sg[0].getAdminEmpNum()); //convert int to string by calling the static method toString()
            toprint[2][i] = Integer.toString(sg[0].getTeachersNum());
            toprint[3][i] = Integer.toString(sg[0].getStudentsNum());
        }

//        for (int i = 0; i < 4; i++) {
//            for (int j = 0; j < 4; j++) {
//                System.out.print(toprint[i][j] + "           |    ");
//            }
//            System.out.println();
//
//        }
        PrintToprintArray(toprint, 4, 4);

//        String[][] toprint = new String[4][4];
        toprint[0][0] = "Middle Schools";
        toprint[1][0] = "Admins";
        toprint[2][0] = "Techers";
        toprint[3][0] = "Students";

        for (int i = 1; i <= regions.length; i++) {
            SchoolGroup[] sg = regions[i - 1].getSchoolGroup();
            toprint[0][i] = regions[i - 1].getRegionName();
            toprint[1][i] = Integer.toString(sg[1].getAdminEmpNum()); //convert int to string by calling the static method toString()
            toprint[2][i] = Integer.toString(sg[1].getTeachersNum());
            toprint[3][i] = Integer.toString(sg[1].getStudentsNum());
        }
            System.out.println();
            System.out.println();
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                System.out.print(toprint[i][j] + "           |    ");
            }
            System.out.println();

        }
        toprint[0][0] = "High Schools";
        toprint[1][0] = "Admins";
        toprint[2][0] = "Techers";
        toprint[3][0] = "Students";

        for (int i = 1; i <= regions.length; i++) {
            SchoolGroup[] sg = regions[i - 1].getSchoolGroup();
            toprint[0][i] = regions[i - 1].getRegionName();
            toprint[1][i] = Integer.toString(sg[2].getAdminEmpNum()); //convert int to string by calling the static method toString()
            toprint[2][i] = Integer.toString(sg[2].getTeachersNum());
            toprint[3][i] = Integer.toString(sg[2].getStudentsNum());
        }
            System.out.println();
            System.out.println();
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                System.out.print(toprint[i][j] + "           |    ");
            }
            System.out.println();

        }
        
    }

    //Query 8.Given the district, list the number of admin employees or teachers or students.
    public static void NumberOfEmployeeAndTeacherAndStudentInDistrict() {
        Scanner input = new Scanner(System.in);
        District district = getUserDistrict();
        System.out.println("Choose Type of people: ");
        System.out.println("1- Admins");
        System.out.println("2- Techers");
        System.out.println("3- Students");
        System.out.print("Your choice:  ");
        int answer = input.nextInt();

        String[][] toprint = new String[2][4];
        toprint[0][0] = district.getDistrictName();
        toprint[0][1] = "# in Primary School";
        toprint[0][2] = "# in Middle School";
        toprint[0][3] = "# in High School";
        if (answer == 1) {
            toprint[1][0] = "Admins";
        } else if (answer == 2) {
            toprint[1][0] = "Teachers";
        } else {
            toprint[1][0] = "Students";
        }

        Region[] regions = district.getRegions();
        int[] temp = {0, 0, 0}; //temporary array to put the sum of peopel in
        for (int i = 1; i <= regions.length; i++) {
            SchoolGroup[] sg = regions[i - 1].getSchoolGroup();
            if (answer == 1) {
                temp[0] += sg[0].getAdminEmpNum();
                temp[1] += sg[1].getAdminEmpNum();
                temp[2] += sg[2].getAdminEmpNum();
            } else if (answer == 2) {
                temp[0] += sg[0].getTeachersNum();
                temp[1] += sg[1].getTeachersNum();
                temp[2] += sg[2].getTeachersNum();
            } else {
                temp[0] += sg[0].getStudentsNum();
                temp[1] += sg[1].getStudentsNum();
                temp[2] += sg[2].getStudentsNum();
            }
        }

        toprint[1][1] = Integer.toString(temp[0]);
        toprint[1][2] = Integer.toString(temp[1]);
        toprint[1][3] = Integer.toString(temp[2]);

        PrintToprintArray(toprint, 2, 4);

//        for (int i = 0; i < 2; i++) {
//            for (int j = 0; j < 4; j++) {
//                System.out.print(toprint[i][j] + "           |    ");
//            }
//            System.out.println();
//
//        }
    }
//ery  9.Given the district, region, list the number of admin employees or teachers or students

    public static void NumberOfOneKindOFpplInRegion() {
        District district = getUserDistrict();
        Scanner input = new Scanner(System.in);
        System.out.println();

        Region regions = getUserRegion();
        SchoolGroup[] sg = regions.getSchoolGroup();
        boolean x = true;
        int answer = 0;
        try {
            System.out.println("Choose Type of people: ");
            System.out.println("1- Admins");
            System.out.println("2- Techers");
            System.out.println("3- Students");
            System.out.print("Your choice:  ");
            answer = input.nextInt();
            x = false;
        }
 catch (InputMismatchException e) {

            System.err.println("Invalid Input , plz enter one of the numbers in list above");
        }
    
        String[][] toprint = new String[2][4];
        toprint[0][0] = district.getDistrictName();
        toprint[0][1] = "# in Primary School";
        toprint[0][2] = "# in Middle School";
        toprint[0][3] = "# in High School";
        if (answer == 1) {
            toprint[1][0] = "Admins";
            toprint[1][1] = Integer.toString(sg[0].getAdminEmpNum());
            toprint[1][2] = Integer.toString(sg[1].getAdminEmpNum());
            toprint[1][3] = Integer.toString(sg[2].getAdminEmpNum());

        } else if (answer == 2) {
            toprint[1][0] = "Teachers";
            toprint[1][1] = Integer.toString(sg[0].getTeachersNum());
            toprint[1][2] = Integer.toString(sg[1].getTeachersNum());
            toprint[1][3] = Integer.toString(sg[2].getTeachersNum());
        } else {
            toprint[1][0] = "Students";
            toprint[1][1] = Integer.toString(sg[0].getStudentsNum());
            toprint[1][2] = Integer.toString(sg[1].getStudentsNum());
            toprint[1][3] = Integer.toString(sg[2].getStudentsNum());
        }

        PrintToprintArray(toprint, 2, 4);
//        for (int i = 0; i < 2; i++) {
//            for (int j = 0; j < 4; j++) {
//                System.out.print(toprint[i][j] + "           |    ");
//            }
//            System.out.println();
//
//        }
    }
//Query 10.Given the district, region, school level, list the number of admin employees, teachers and students

    public static void NumberOfPPLInOneSchoolLevelInRegion() {
        District district = getUserDistrict();
        Scanner input = new Scanner(System.in);
        System.out.println();

        Region regions = getUserRegion();

        SchoolGroup[] sg = regions.getSchoolGroup();

        System.out.println("1. Primary School");
        System.out.println("2.Intermediate School ");
        System.out.println("3.High School");
        System.out.println("Choose the SchoolType : ");
        int userAnswer = input.nextInt();

        SchoolGroup userSG = sg[userAnswer - 1];

        String[][] toprint = new String[2][4];
        toprint[0][1] = "Admin";
        toprint[0][2] = "Teachers";
        toprint[0][3] = " Student";
        toprint[1][0] = "Number";
        if (userAnswer == 1) {
            toprint[0][0] = "Primary School";
        } else if (userAnswer == 2) {
            toprint[0][0] = "Intermediate School";
        } else if (userAnswer == 3) {
            toprint[0][0] = "High School ";
        }
        toprint[1][1] = Integer.toString(userSG.getAdminEmpNum());
        toprint[1][2] = Integer.toString(userSG.getTeachersNum());
        toprint[1][3] = Integer.toString(userSG.getStudentsNum());

        PrintToprintArray(toprint, 2, 4);

//        for (int i = 0; i < 2; i++) {
//
//            for (int j = 0; j < 4; j++) {
//
//                System.out.print(toprint[i][j] + "   |    ");
//            }
//            System.out.println();
//        }
    }

    // function print the Districts and let the user choose

    public static District getUserDistrict() {
        boolean x = true;
        int answer = 0;
        try {

            for (int i = 0; i < DistrictList.length; i++) {
                System.out.println((i + 1) + "- " + DistrictList[i].getDistrictName());
            }
            Scanner input = new Scanner(System.in);
            System.out.println("Choose a district: ");
            answer = input.nextInt();

            x = false;
        } catch (NumberFormatException e) {
            System.err.println(" Invalid input , plz choose one of the numbers in list above");
        }
        return DistrictList[answer - 1];
    }

// function print the Regions and let the user choose

    public static Region getUserRegion() {
        //boolean x = true ;
        int answer = 0;
        try {
            Scanner input = new Scanner(System.in);
            for (int i = 0; i < regions.length; i++) {
                System.out.println((i + 1) + "- " + regions[i].getRegionName());
            }
            System.out.println("Choose Region : ");
            answer = input.nextInt();


            //x = false ;
        } catch (NumberFormatException e) {
            System.err.print("" + e + " , invalide input , "
                    + "plz input of othe list number above ");
        }
        return regions[answer - 1];
    }

    
 //funtion print the toprint array which takes 2D array and the Limites 

    public static void PrintToprintArray(String[][] toprint, int Ilimit, int Jlimit) {
        for (int i = 0; i < Ilimit; i++) {
            for (int j = 0; j < Jlimit; j++) {
                System.out.print(toprint[i][j] + "      |    ");
            }
            System.out.println();
        }
    }
}

you can find the dealing with the user in " for example "

public static District getUserDistrict() {
        boolean x = true;
        int answer = 0;
        try {

            for (int i = 0; i < DistrictList.length; i++) {
                System.out.println((i + 1) + "- " + DistrictList[i].getDistrictName());
            }
            Scanner input = new Scanner(System.in);
            System.out.println("Choose a district: ");
            answer = input.nextInt();

            x = false;
        } catch (NumberFormatException e) {
            System.err.println(" Invalid input , plz choose one of the numbers in list above");
        }
        return DistrictList[answer - 1];
    }

// function print the Regions and let the user choose

    public static Region getUserRegion() {
        //boolean x = true ;
        int answer = 0;
        try {
            Scanner input = new Scanner(System.in);
            for (int i = 0; i < regions.length; i++) {
                System.out.println((i + 1) + "- " + regions[i].getRegionName());
            }
            System.out.println("Choose Region : ");
            answer = input.nextInt();


            //x = false ;
        } catch (NumberFormatException e) {
            System.err.print("" + e + " , invalide input , "
                    + "plz input of othe list number above ");
        }
        return regions[answer - 1];
    }

and 2D array in :: " for example "

public static void NumberOfOneTypeOfSchoolsInOneRgion() {
        District district =getUserDistrict();

        Region regions = getUserRegion();


        SchoolGroup[] sg = regions.getSchoolGroup();
        // making 2D array
        String[][] toprint = new String[2][4];
        toprint[0][0] = regions.getRegionName();
        toprint[0][1] = "Primary School";
        toprint[0][2] = "Middle School";
        toprint[0][3] = "High School";
        toprint[1][0] = "# of schools";
        toprint[1][1] = Integer.toString(sg[0].getNumOfSchools());
        toprint[1][2] = Integer.toString(sg[1].getNumOfSchools());
        toprint[1][3] = Integer.toString(sg[2].getNumOfSchools());

         PrintToprintArray(toprint, 2, 4);
        /*
         
         */
//        for (int i = 0; i < 2; i++) {
//            for (int j = 0; j < 4; j++) {
//                System.out.print(toprint[i][j] + "           |    ");
//            }
//            System.out.println();
//
//        }
    }

Now , My specific Q is :: how can ask the user and take her/his answer in Jframe , and print the 2D array in JTable form ?


thanks alot :')

Best Regards ,

Recommended Answers

All 13 Replies

To ask the user a question and wait for the answer you would not normally use a JFrame, the class JOptionPane is the normal way to do GUI dialog boxes to ask questions. Details are in the API doc in the usual places.
You will find instructions for using JTable in the Sun/Oracle tutorials here
http://download.oracle.com/javase/tutorial/uiswing/components/table.html#simple
because your already have you info in a String[][] you can use the simplest form of JTable. Just create an array of column names and then
JTable table = new JTable(toprint, columnNames);
Add the JTable to your JFrame and that's it.

Mmmmmm ok let's try it?

I used the JOptionPane from the swing in this function but it doesn't work :(

I wrote it like this :

public static Region getUserRegion() {
        //boolean x = true ;
        int answer = 0;
        try {
            Scanner input = new Scanner(System.in);
            for (int i = 0; i < regions.length; i++) {
                System.out.println((i + 1) + "- " + regions[i].getRegionName());
            }
//            System.out.println("Choose Region : ");
//            answer = input.nextInt();
            answer  =Integer.parseInt(JOptionPane.showInputDialog(null,"Choose the number of the region" ));

            //x = false ;
        } catch (NumberFormatException e) {
            System.err.print("" + e + " , invalide input , "
                    + "plz input of othe list number above ");
        }
        return regions[answer - 1];
    }

no window , and when I input number it stop working , it doesn't complete it works ( the method I mean " !

The InputDialog looks OK to me. Do you get the dialog appearing?
Try printing answer immediately after calling it (after line 12)

I copied your exact code as follows

int answer  =Integer.parseInt(JOptionPane.showInputDialog(null,"Choose the number of the region" ));
      System.out.println(answer);

and it shows the dialog, then prints whatever integer value I enter into it.

and about the Jtable .. I know how to use it when I've exact value to put in the Table
but in this program , I have to deal with the user and take some info from her/him

would u plz pick any of the functions above and show me how to use it ?


thanks a lot bro :) I truly appreciated this help from you

May God bless you :-)

Sorry :( it still doesn't work !

no dialog , and the function stop it works

Sorry :( it still doesn't work !

no dialog , and the function stop it works

Put a print immediately before it to check its being executed. Change the catch to catch all Exception rather than just NumberFormatException. It really should work!

I wrote it like this :

public static Region getUserRegion() {
        //boolean x = true ;
        int answer = 0;
        try {
            Scanner input = new Scanner(System.in);
            for (int i = 0; i < regions.length; i++) {
                System.out.println((i + 1) + "- " + regions[i].getRegionName());
            }
           // System.out.println("Choose Region : ");
           // answer = input.nextInt();
         //   answer  =Integer.parseInt(JOptionPane.showInputDialog(null,"Choose the number of the region" ));
          answer  =Integer.parseInt(JOptionPane.showInputDialog(null,"Choose the number of the region" ));
          System.out.println(answer);

            //x = false ;
        } catch (Exception e) {
            System.err.print("" + e + " , invalide input , "
                    + "plz input of othe list number above ");
        }
        return regions[answer - 1];
    }

and still doesn't woke !!!!

Put a print immediately before it to check its being executed (just before line 12)

and about the Jtable .. I know how to use it when I've exact value to put in the Table
but in this program , I have to deal with the user and take some info from her/him

would u plz pick any of the functions above and show me how to use it ?


thanks a lot bro :) I truly appreciated this help from you

May God bless you :-)

Keep the existing code up to the point where you have your toprint array populated with data then, instead of printing it to System.out,
create a JTable like I said in my previous post
create new JFrame
add the table to the frame
make the frame visible

... it really is that easy.

you mean like this ?

public static Region getUserRegion() {
        //boolean x = true ;
        int answer = 0;
        try {
            Scanner input = new Scanner(System.in);
            for (int i = 0; i < regions.length; i++) {
                System.out.println((i + 1) + "- " + regions[i].getRegionName());
            }
//            System.out.println("Choose Region : ");
//            answer = input.nextInt();
         //   answer  =Integer.parseInt(JOptionPane.showInputDialog(null,"Choose the number of the region" ));
           System.out.println(answer);
          answer  =Integer.parseInt(JOptionPane.showInputDialog(null,"Choose the number of the region" ));
          

            //x = false ;
        } catch (Exception e) {
            System.err.print("" + e + " , invalide input , "
                    + "plz input of othe list number above ");
        }
        return regions[answer - 1];
    }

it print me 0 ! and then stop working again !!

let's done with the Jopation pane and then see what to do with JFrame :)

Maybe a silly question but... the JOptionPane isn't opening behind another window is it? I've never had a problem with it not displaying properly.

Mmmmm sorry , english is not my native language , what do u mean ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.