0

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!))

Community
  • 1
  • 1
Sue
  • 199
  • 1
  • 3
  • 10
  • your question and sample code is not clear. – vels4j Oct 24 '13 at 12:45
  • 1
    Have you tried debugging your code? – rlegendi Oct 24 '13 at 14:53
  • Yeah your adds are basically going nowhere. `ClassName classO = new ClassName(); classO.addClass(virtualObject);` Your list of the classes needs to be a field in the outer class. – Radiodef Oct 25 '13 at 05:18
  • I've added some relevant code in the question. and no, I haven't debug it. Will do later – Sue Oct 25 '13 at 05:18
  • btw thanks @Radiodef! You helped me alot – Sue Oct 25 '13 at 05:20
  • Declare the ArrayList inside the class but outside the methods. : ) I forget what your original program structure looked like but you had a second class that was keeping track of the list of names. You could keep that same structure, just keep the list of classes instead of a list of names. Also no problem. – Radiodef Oct 25 '13 at 05:21
  • Okay what I mean is that class0 is only inside the scope of the method. So you can either have the ClassName object (that I assume keeps the list) as a field in the main class of the application (it looks like the event handler is an inner class of this) or you can just keep the list as a field if you aren't wanting to add to the ClassName class. – Radiodef Oct 25 '13 at 05:29
  • Seems like you have more than one problem. I am over in the Java chat if you want to work this out since it doesn't seem like there is a clear one question to answer. – Radiodef Oct 25 '13 at 05:32
  • SO says that my reputation is not enough to chat! :( – Sue Oct 25 '13 at 09:31
  • I've set the `classO` and `virtualObject` outside the method and inside the class, but it doesn't work either. – Sue Oct 25 '13 at 13:33
  • @Radiodef I've solved part of the problem! :P I've edited my code according to this post: http://stackoverflow.com/questions/15377312/create-new-object-in-arraylist-with-attributes, and now the `checkName()` finally works. however ...there' s still other problems...that is out of scale of this question. Ah Java is sooo hard – Sue Oct 25 '13 at 16:49
  • If you still are having problems probably the relevant code to post is just the entire source code. It looks like pretty much your list is empty but it's unclear to me why. I see some small things that look like they could be errors but I can't say for sure without seeing how it all fits together. – Radiodef Oct 26 '13 at 18:14
  • I've edited the program and now I am getting stackOverFlow error. I can't even run the programme so I don't know if value can be saved...maybe I should post a new question and post my whole programme? :( – Sue Oct 27 '13 at 08:26
  • Posted a new question!!http://stackoverflow.com/questions/19616369/unknown-stackoverflow-error-which-may-be-related-to-class-object-arraylist-dat Please kindly take a look :) Thank you – Sue Oct 27 '13 at 09:11

0 Answers0