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

Time:04-08

I have an assignment I'm working on, and I need to basically make a system that checks if a string entered already exist in an ArrayList. In the case that it does not exist, it will make a new integer with the name of the exact entered text, and assign the value to 1. If the string already does exist (meaning someone else already entered that choice) then it will add one to the already existing integer. I'm just struggling with assigning an integer with the value of the string.

I've tried directly taking the input and assigning it to a string, to which I then try to use the string value to create a new integer. This has been unsuccessful so far.

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

CodePudding user response:

I guess this assignment has some pre-defined flavors (chocolate, vanilla etc) and they have designated button to vote for. Also it allows you to add your custom flavors to the collection and counts for its vote.

You cannot initialize variable during runtime. It is because you write the code so the computer run it for you. You cannot write the code so the program will write more new code for by itself. (at least this is not the purpose of this assignment)

In this case I would suggest you to store the flavors and vote to a unified data structure. I can see you store pre-defined flavors in strawberryVotes, chocolateVotes. You can make a map to store all the information you need, pre-defined or custom flavors. You need to store 2 information. 1) flavor name 2) vote. We can make use of a Map. Its concept is similar to a 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. I thinks some of the attributes should not declared as static. But that is another topic.

  • Related