checkName() 和将用户输入存储到 ArrayList 和 boolean[] 的问题 [英] Problems of checkName() and storing user input to ArrayList and boolean[]

查看:40
本文介绍了checkName() 和将用户输入存储到 ArrayList 和 boolean[] 的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我有一个 GUI 窗口,它基本上要求用户选择一个 JRadioButton 并在 JTextField 中输入一些内容,然后选择确认/取消.

Here I have a GUI window and it basically ask the user to select a JRadioButton and type something in a JTextField, then choose confirm/cancel.

这是一个我们必须制作 UML-to-Java 文本文件的项目.用户将输入类信息并选择 UML 关系,该程序必须在 JTextField 上打印出 Java clsas 文本.就像在 eclipse 中创建一个新类一样.

It is a project which we have to make a UML-to-Java text file. User would enter class information and choose a UML relationship, and this programme have to print out the Java clsas text on a JTextField. Just like when you create a new class in eclipse.

我想要做的是创建一个 boolean[] 来存储一个布尔数组,当用户选择 JRadioButton_A 时它会存储真,当用户选择 JRadioButton_B 时它会存储假.而且我想要在 JTextField 中输入的东西由checkName()检查,如果该方法返回false,则该字符串将存储在一个ArrayList中.

what I want to do is make a boolean[] to store an array of booleans, when user selects JRadioButton_A it'll store true and when user select JRadioButton_B it'll store false.And also I want the things typed in JTextField to be checked by a checkName(), if the method returns false, the string will be stored in an ArrayList.

下面是我的代码 - getName() 方法和用于存储真假的 boolean[] 存在一些问题.当用户需要再次输入名称时,它会将丢弃的 sting/boolean 保存到数组中.(对不起,我的英语不好!)有没有更好的方法来制作这个程序?我觉得我把事情复杂化了,应该有一个更简单的方法.

Below is my code - there's some problems in getName() method and the boolean[] for storing true and false. When user needs to input name again, it would save the discarded sting/boolean into the array. (Sorry for my bad english!) Is there any better way to make this programme? I feel like I am complicating things and there should be a simpler way to make it.

这是要求用户输入课程信息的 UI 内容.用户必须选择公共/私有,然后输入类名和 JTextField

Here's the UI stuffs asking user to enter class information. User have to select public/private and then type in class name and JTextField

private class Handler implements ActionListener{
    public void actionPerformed(ActionEvent event){
        String name = inputClassName.getText();
        classObject.addName(name);
        while (classObject.checkName(name) == true){
            JOptionPane.showMessageDialog(null, "Class name invalid. " +
                    "\nEntered name should not contain java keywords or equal to other existing names. " +
                    "\nPlease try again."); // doesn't work
            name = inputClassName.getText();
            classObject.addName(name);
        }// end if
        JOptionPane.showMessageDialog(null, "Class saved."); // doesn't work
        name = inputClassName.getText();
        classObject.addName(name);

    }// end actionPerformed()
}// end Handler class

private class Handler2 implements ActionListener{
    public void actionPerformed(ActionEvent event){
        boolean b = true;
        b = classObject.setPP();
        }
    }

private class Handler3 implements ActionListener{
    public void actionPerformed(ActionEvent event){
        boolean b = false;
        b = classObject.setPP();
        }
    }

这是将输入存储到 ArrayList 和 boolean[] 的方法

Here's the methods for storing the inputs to the ArrayList and boolean[]

Scanner input = new Scanner(System.in);
JavaKeywords keyObject = new JavaKeywords();

private ArrayList<String> className = new ArrayList<String>();
private String name = new String();
private int size = className.size();
private Boolean[] bArray = new Boolean[size];


public boolean checkName(String name){
    boolean check = true;
    for (int i=0; i<=size; i++){
        if (keyObject.containsKeyword(className.get(i)) || name.equals(className.get(i))){

            boolean o = false;
            check = o;
        }// end if
    }// end for
    return check;
}// end checkName
    
public boolean setPP(){
    boolean b = true;
    return b;
}

public void addPP(Boolean[] bArray){
    this.bArray = bArray;
    for (int i=0; i>=size; i++){
        bArray[i] = setPP();
    }
}// add a Array of boolean. for className[i], its class type = item[i] in bArray. 
             // public = true, private = false
public String getPublicPrivate(){
    String p = "";
    for (int i =0; i<=size; i++){
        if(bArray[i]=true)
            p = "public";
        else
            p = "private";
    }
    return p;
}


已解决

解决方案:将string classNameboolean isPrivate存储在一个类中,并把这个类变成一个ArrayList所有的麻烦.但是后来我遇到了另一个问题,即在我更改代码后 checkName() 不起作用.这是 ActionListener


Solved

Solution: store the string className and boolean isPrivate in a class and make the class into an ArrayList can save me from all the trouble. But then i faced anther problem, that is the checkName() doesn't work after I changed my code. here is the ActionListener

private class Handler implements ActionListener{
    public void actionPerformed(ActionEvent event){
        
        VirtualClass virtualObject = new VirtualClass();
        classObject.addClass(virtualObject);
        String name = inputClassName.getText();
        virtualObject.className = name;
        
        if (classObject.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()

这里是checkName()方法

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

对于 checkName() 中的 containsKeyword() 我使用了来自 如何检查类名是否有效? @MrLore.

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

推荐答案

我可能要做的是创建一个简单的类来表示您的字段,这样您就根本不必使用多个列表.

Probably what I would do is create a simple class to represent your fields so you don't have to use multiple lists at all.

public class VirtualClass {
    public boolean isPrivate;
    public String className = "Object";
}

ArrayList<VirtualClass> classes = new ArrayList<VirtualClass>(0);

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

否则,您将不得不创建某种类型的第二个列表来保存公共/私人.您只需同时更改它们.

Otherwise you will have to create a second list of some kind to hold the public/private. You will just have to change them in parallel.

// in actionPerformed

ClassObject.VirtualClass clazz = new ClassObject.VirtualClass();

clazz.isPrivate = rbPrivate.isSelected();
clazz.className = tfClassName.getText();

classObject.addClass(clazz);

忽略单选按钮的收听,因为从技术上讲,在将类添加到列表之前,您不需要它们的状态.

And just ignore the listening on the radio buttons since you technically do not need their states until you go to add the class to the list.

要稍后访问这些字段,您只需

To access the fields later you just need to

for (VirtualClass clazz : classes) {
    System.out.println((clazz.isPrivate ? "private" : "public") + " " + clazz.className);
}

// or something like

for (int i = 0; i < classes.size(); i++) {
    System.out.println(classes.get(i).className + ":");
    if (classes.get(i).isPrivate) {
        System.out.println(" private");
    } else {
        System.out.println(" public");
    }
}

这篇关于checkName() 和将用户输入存储到 ArrayList 和 boolean[] 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆