0

So I'm trying to make a code generator that will generate setter and getter methods if you input variable names like: "int nums" or "String words" etc.

My problem is that it can only take one input

If you put more than one variable it will give me this:

Input

int nums
String words

Output

public int set nums
string()
{
    return nums
string;
}

public int get nums
string()
{
    return nums
string;
}

My approach was to create two text files with the templates for static and nonstatic methods, for example they look like this:

public <<Variable>> set <<Name>>()
{
    return <<Name>>;
}

public <<Variable>> get <<Name>>()
{
    return <<Name>>;
}

I know that the setter isn't supposed to return anything, but I am just trying to get this first step to work before I move on.

Then I store these in a string and get input from the user and use their input to replace the <> and the <> elements of the string. Here is my code Note: I am using Swing to make this a GUI

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.util.*;
import java.io.*;

public class CodeGenerator {


//Private Variables and Methods

private boolean checked = false;
private String staticTemplate = null;
private String nonStaticTemplate = null;
private ArrayList<String> input = new ArrayList<String>();






 /**
  * Will Replace The Parts Of The Text Documents With the Variable Type And Name And Repeat For All Of The Variables In The Text Area. 
  * It Will Then Put The Result In The Output Text Area For The User To Copy And Paste
  */
 public static void replace(ArrayList<String> inputs, String methods, JTextArea output)
 {
     for(int i = 0; i < inputs.size(); i+=2)
     {
         methods = methods.replaceAll("<<Variable>>", inputs.get(i));
     }

     for(int i = 1; i < inputs.size(); i+=2)
     {
         methods = methods.replaceAll("<<Name>>", inputs.get(i));
     }

     output.setText(methods);


 }





private JFrame frame;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                CodeGenerator window = new CodeGenerator();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public CodeGenerator() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setResizable(false);
    frame.setBounds(100, 100, 526, 617);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    //This is the place where the generated code will be output
    JTextArea generatedCodeArea = new JTextArea();
    generatedCodeArea.setEditable(true);
    generatedCodeArea.setBounds(10, 140, 490, 367);
    frame.getContentPane().add(generatedCodeArea);

    //This is where text will be entered
    JTextArea inputTextArea = new JTextArea();
    inputTextArea.setBounds(10, 11, 490, 123);
    frame.getContentPane().add(inputTextArea);

    //This is the checkbox for static and non static setter/getter
    JCheckBox nonStatic = new JCheckBox("Non-Static Setter/Getter");
    nonStatic.setBounds(20, 514, 150, 23);
    frame.getContentPane().add(nonStatic);
    nonStatic.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            //set the checked variable to use in the action event for the generate button
            boolean selected = nonStatic.isSelected();

            if(selected == true)
            {
                checked = true;
            }else if(selected == false)
            {
                checked = false;
            }
        }

    });


    //Generate Button
    JButton btnGenerate = new JButton("Generate");
    btnGenerate.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {


            generatedCodeArea.setText("");


            //Store the tempaltes into Strings to be edited later
            try
            {
                staticTemplate = new Scanner(new File("staticGetterSetter.txt")).useDelimiter("\\Z").next();
                nonStaticTemplate = new Scanner(new File("nonStaticGetterSetter.txt")).useDelimiter("\\Z").next();
            } catch(IOException q)
            {
                generatedCodeArea.setText("Something went wrong... " + q.getMessage());
            }
            //Store the input into a String array
            String[] temp = inputTextArea.getText().split(" ");

            //Then add it to an ArrayList
            for(String word : temp)
            {
                input.add(word);
            }


            //Check to see if the checkbox is checked or not
            if(checked == true)
            {
                //do the non-static setter/getter --> WORKS
                replace(input, staticTemplate, generatedCodeArea);



            }else if(checked == false)
            {
                //do the static setter/getter -- WORKS
                replace(input, nonStaticTemplate, generatedCodeArea);



            }
        }
    });
    btnGenerate.setBounds(215, 518, 179, 49);
    frame.getContentPane().add(btnGenerate);




}
private static void addPopup(Component component, final JPopupMenu popup) {
    component.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            if (e.isPopupTrigger()) {
                showMenu(e);
            }
        }
        public void mouseReleased(MouseEvent e) {
            if (e.isPopupTrigger()) {
                showMenu(e);
            }
        }
        private void showMenu(MouseEvent e) {
            popup.show(e.getComponent(), e.getX(), e.getY());
        }
    });
  }
}

How do I fix the issue?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
ghost1349
  • 83
  • 1
  • 10

1 Answers1

2

The cause of your problem is...

 String[] temp = inputTextArea.getText().split(" ");

if you use the input of

int nums
String words

Then you will end up with an array of something like...

[0] int
[1] nums\nString
[2] words

...which isn't really what you want...

You first need to split the input into lines...

 String[] lines = inputTextArea.getText().split("\n");

Then, for each line, split it into words...

for (String line : lines) {
    String[] temp = line.split(" ");
}

I would then either use output.append(methods) or change the replace method to return a String...

ps: I know this might be a "personal" project but, you really should avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify.

See Why is it frowned upon to use a null layout in SWING? for some more details...

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366