Failed to store variables in a ArrayList of class

2019-08-31 14:22发布

So this is a programme of UML-toJava converter. The user has to input class name and class type(public/ private), and have the variables stored in virtual class. I've made an ArrayList of class object to store users' input, String className and boolean isPrivate. Later the class name and type has to be printed in a GUI window for user to copy the text.

Below is the relevant code.

And more relevant code in the end of this post. and I am pretty busy now, will discribe the problem in detail later. thanks for comment!

GUI for user to enter class infomation -

private class Handler implements ActionListener{
    public void actionPerformed(ActionEvent event){

        VirtualClass virtualObject = new VirtualClass();
        ClassName classO = new ClassName();
        classO.addClass(virtualObject);
        String name = inputClassName.getText();
        virtualObject.className = name;

        if (classO.checkName(name) == false){
            JOptionPane.showMessageDialog(null, "Class name invalid. " +
                    "\nEntered name should not contain java keywords or equal to other existing names. " +
                    "\nPlease try again."); // Always return "invalid" message 
        } else {
            JOptionPane.showMessageDialog(null, "Class saved."); 
            name = inputClassName.getText();
            virtualObject.className = name;
        }

        if (event.getSource() == publicButton) {
            virtualObject.isPrivate = false;
        } else if (event.getSource() == privateButton) {
            virtualObject.isPrivate = true;
        }

    }// end actionPerformed()

}// end Handler class

Here is the virtual class

public class VirtualClass {

    public boolean isPrivate;
    public String className = "class name";


}

Here's the method to add a new virtual class

ArrayList<VirtualClass> classes = new ArrayList<VirtualClass>();
private int size = classes.size();

public void addClass(VirtualClass clazz){
      classes.add(clazz);
}

here's the method to turn the boolean isPrivate into a String for display on screen.

public String setPublic(){
    String s = "s";
    for (int i = 0; i < classes.size(); i++) {
        if (classes.get(i).isPrivate) 
            s = " private";
        else 
            s = "public";
    }
    return s;
}

And here's the method to generate the class information into String javaTextFile

public class GenJava {

ClassName classObject = new ClassName();
VirtualClass virtualObject = new VirtualClass();

String javaTextFile = (classObject.setPublic()+" "+virtualObject.className+"{\n}");

}

The error: when i opened the window which supposed to display String javaTextFile in a JTextArea, only s class name{ } is displayed. so I guess nothing is stored in my ArrayList of virtual class? Please help me to see what's wrong.

more relevant code:

and here is the checkName() method i used to check if the class name is equal to any java keywords or existing name in list

public boolean checkName(String name){
    boolean check = true;
    for (int i=0; i<=size; i++){
        if (keyObject.containsKeyword(classes.get(i).className) || name.equals(classes.get(i).className)){
            boolean o = false;
            check = o;
        }// end if
    }// end for
    return check;
}// end checkName

For containsKeyword() in checkName() I've used a JavaKeywords class from How to check if the class name is valid? by @MrLore.

(here's more code & problem, which may not be relevant: Problems of checkName() and storing user input to ArrayList and boolean[] (solved!))

0条回答
登录 后发表回答