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?