mardi 28 avril 2020

Joptionpane and equalstoignorecase is giving me an invalid output

My JOption Pane show messagedialog keeps on giving me a weird output. My if else statement with equalstoignore also doesnt work. The JOptionPane is giving me a long pane with text such as Name:Javax.swing.JtextField[,0,18,294,19,invalid,layout.............. and its so long i. it doesnt output my desired output like i use on toString.


    public static void main(String[] args)
    {
        ArrayList<PreSchool> PSList = new ArrayList<PreSchool>();
        JTextField  field1 = new JTextField();
        JTextField  field2 = new JTextField();
        JTextField  field3 = new JTextField();

        Object[] fields = {
            "Please Enter Name:",field1,
            "Please Enter Race(Malay/Chinese/Indian):", field2,
            "Please Enter Age:", field3
        };

        for(int i = 0; i < 5;i++){
            JOptionPane.showConfirmDialog(null, fields,"Enter Information for Children " + i,JOptionPane.OK_CANCEL_OPTION );
            String name = field1.toString();
            String race = field2.toString();
            int age = Integer.parseInt(field3.getText());
            PSList.add(new PreSchool(name,race,age));
        }

        StringBuilder builder = new StringBuilder();
        builder.append("Index: 0\n");
        builder.append(PSList.get(0).toString());
        builder.append("Index: 1\n");
        builder.append(PSList.get(1).toString());
        builder.append("Index: 2\n");
        builder.append(PSList.get(2).toString());
        builder.append("Index: 3\n");
        builder.append(PSList.get(3).toString());
        builder.append("Index: 4\n");
        builder.append(PSList.get(4).toString());

        JOptionPane.showMessageDialog(null,builder);

        int malay =0;
        int chinese = 0;
        int indian = 0;
        for(int i = 0; i < 5; i++)
        {
            if(PSList.get(i).getRace().equalsIgnoreCase("Malay"))
            {
                malay++;
            }
            else 
            {
                if(PSList.get(i).getRace().equalsIgnoreCase("Chinese"))
                {
                    chinese++;
                }
                else
                {
                    indian++;
                }
            }

        }
        JOptionPane.showMessageDialog(null,"Number of Malay:" + malay + "\nNumber of Chinese:" + chinese + "\nNumber of Indian:" + indian);


    }


}

Here is my Object class

    String name;
    String race;
    int age;

    public PreSchool()
    {
        name = " ";
        race = " ";
        age = 0;
    }

    public PreSchool(String n, String r,int a)
    {
        name =n;
        race =r;
        age = a;
    }

    public String getName() {
        return name;
    }

    public String getRace() {
        return race;
    }

    public int getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "Name:" + name + ", race:" + race + ", age:" + age + '\n';
    }


}

Aucun commentaire:

Enregistrer un commentaire