如何用字符串的输入值命名一个新整数?

How can I name a new integer with the input value of a String?

我有一项作业正在处理,我基本上需要制作一个系统来检查输入的字符串是否已存在于 ArrayList 中。如果它不存在,它会生成一个新的整数,其名称与输入的文本完全相同,并将值赋给 1。如果该字符串已经存在(意味着其他人已经输入了该选择),那么它将添加一到已经存在的整数。我只是在努力为字符串的值分配一个整数。

我试过直接获取输入并将其分配给一个字符串,然后我尝试使用该字符串值创建一个新的整数。到目前为止,这一直没有成功。

static int totalSubmissions = 0;  // tracks # of custom submissions (used to navigate Array List)
static ArrayList<String> userInputs = new ArrayList<String>(); // contains strings (not listed ice                     creams) from user input
private static JLabel pollOtherLabel = new JLabel("Unlisted Flavors: ");
private static JTextField customInput = new JTextField();
private static JButton submitInput = new JButton("Submit");

// Above is the totalSubmmissions (used to navigate ArrayList, the list itself, and my label, submit button, and the neccesary code to record what was entered.




@Override
        public void actionPerformed(ActionEvent e) {
            // checks vote chocolate and contains output
            if(e.getSource() == voteChocolate){
                chocolateVotes++;
                pollOption1Label.setText("Chocolate Votes: " + chocolateVotes);
            } else if (e.getSource() == voteVanilla){ // checks vote vanilla and contains output
                vanillaVotes++;
                pollOption2Label.setText("Vanilla Votes: " + vanillaVotes);
            } else if (e.getSource() == voteStrawberry){  // checks vote strawberry and contains output
                strawberryVotes++;
                pollOption3Label.setText("Strawberry Votes: " + strawberryVotes);
            } else if (e.getSource() == submitInput){ // checks custom input and contains output
                String currentString = customInput.getText(); //  <<  ISSUE IS HERE 
                if (userInputs.contains(currentString)){     // want to assign the text field input 
                    int currentString = 1;                 // to a new integer, that will track
                } else {                         // the number of times that exact string is entered 
                    int customInput.getText() = 1;
                }
                userInputs.add(customInput.getText());
                customInput.setText("");
                pollOtherLabel.setText("Unlisted Flavors: " + userInputs.get(totalSubmissions));
                totalSubmissions++;
            }
        }

我猜这个作业有一些 pre-defined 口味(巧克力、香草等),他们有指定的按钮来投票。它还允许您将自定义口味添加到集合中并计入其投票。

您不能在 运行 时间内初始化变量。就是因为你写的代码所以计算机运行才替你。您无法编写代码,因此程序将自行编写更多新代码。 (至少这不是这次作业的目的)

在这种情况下,我建议您存储口味并投票给统一的数据结构。我可以看到你在 strawberryVoteschocolateVotes 中存储了 pre-defined 种口味。您可以制作一张地图来存储您需要的所有信息,pre-defined 或自定义口味。您需要存储 2 条信息。 1) 口味名称 2) 投票。我们可以使用地图。它的概念类似于table.

    static int totalSubmissions = 0; // tracks # of custom submissions (used to navigate Array List)

    private Map<String, Integer> flavorVotes = new HashMap<String, Integer>();
    // contains flavor and vote (not listed ice creams) from user input

    private static JLabel pollOtherLabel = new JLabel("Unlisted Flavors: ");
    private static JTextField customInput = new JTextField();
    private static JButton submitInput = new JButton("Submit");

    // define constant for predefined flavor
    private static final String FLAVOR_CHOCOLATE = "Chocolate";
    private static final String FLAVOR_VANILLA_ = "Vanilla";
    private static final String FLAVOR_STRAWBERRY_ = "Strawberry";

    // Above is the totalSubmmissions (used to navigate ArrayList, the list itself,
    // and my label, submit button, and the neccesary code to record what was
    // entered.

    @Override
    public void actionPerformed(ActionEvent e) {
        String flavor;
        // checks vote chocolate and contains output
        if (e.getSource() == voteChocolate) {
            flavor = FLAVOR_CHOCOLATE;
            addVote(flavor);
            pollOption1Label.setText("Chocolate Votes: " + flavorVotes.get(flavor));
        } else if (e.getSource() == voteVanilla) { // checks vote vanilla and contains output
            flavor = FLAVOR_VANILLA_;
            addVote(flavor);
            pollOption2Label.setText("Vanilla Votes: " + flavorVotes.get(flavor));
        } else if (e.getSource() == voteStrawberry) { // checks vote strawberry and contains output
            flavor = FLAVOR_STRAWBERRY_;
            addVote(flavor);
            pollOption3Label.setText("Strawberry Votes: " + flavorVotes.get(flavor));
        } else if (e.getSource() == submitInput) { // checks custom input and contains output
            flavor = customInput.getText();
            addVote(flavor);

            customInput.setText("");
            pollOtherLabel.setText("Unlisted Flavors: " + flavor);
            totalSubmissions++;
        }
    }

    private void addVote(String flavor) {
        if (flavorVotes.containsKey(flavor)) {
            // flavor exists, increment by 1
            flavorVotes.put(flavor, flavorVotes.get(flavor) + 1);
        } else {
            // new flavor, set its vote to 1
            flavorVotes.put(flavor, 1);
        }
    }

p.s。我认为某些属性不应声明为静态的。不过那是另外一个话题了。