检查名(中)和存储用户输入的问题,ArrayList和布尔[](解决了!) [英] Problems of checkName() and storing user input to ArrayList and boolean[] (solved!)

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

问题描述

所以我在这里有一个GUI窗口,它基本上是要求用户选择一个JRadioButton并输入一个JTextField的东西,然后选择确认/取消。

So 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.

我想要做的就是让一个布尔[]存储布尔值数组,当用户选择JRadioButton_A它会保存真实,当用户选择JRadioButton_B它会存储false.And我也想在JTextField中输入的东西要通过检查名()进行检查,如果该方法返回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.

下面是我的code - 有中的getName一些问题()方法,并用于存储真假布尔[]。当用户需要输入名字再次,它可以拯救丢弃蜇/布尔入阵。 (对不起,我的英文不好!)有没有什么更好的办法,使这个计划?我觉得我的东西复杂化,并应使它成为一个更简单的方法。

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和布尔方法[]

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;
}

解决方案:存储字符串的className 布尔isPrivate 中的一类,使类成的ArrayList 可以从所有的烦恼救我。但后来我面对花药的问题,那就是检查名()之后,我改变了我的code不起作用。
这里是的ActionListener

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

和这里是检查名()

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

有关 containsKeyword()检查名()我已经使用了 JavaKeywords 如何检查类名是有效的?通过@MrLore 的。

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

推荐答案

也许我会做的是创建一个简单的类来重新present您的字段,以便您不必在所有使用多个列表。

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");
    }
}

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

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