Jeric Marquez
Jeric Marquez

Reputation: 73

How do I add up the loaded value from a textfile with the new inputed value in the Jtextfield

When I press the load button it displays the values from the reload.txt

   public class VotingSystem extends javax.swing.JFrame {

private final JTextField[] textFields;

/**
 * Creates new form VotingSystem
 */
public VotingSystem() {
initComponents();

textFields = new JTextField[12];

textFields[0] = koontf;
textFields[1] = baamtf;
textFields[2] = sachitf;
textFields[3] = fakertf;
textFields[4] = phonsekaltf;
textFields[5] = lauretf;
textFields[6] = yeontf;
textFields[7] = aguerotf;
textFields[8] = agnistf;
textFields[9] = lokitf;
textFields[10] = lawliettf;
textFields[11] = ryuzakitf;
} private void loadActionPerformed(java.awt.event.ActionEvent evt) {                                     

int line = 0;
try(Scanner scanner = new Scanner(new File("reload.txt"))){
    while(scanner.hasNextLine()){
        textFields[line++].setText(scanner.nextLine());
        if(line == textFields.length){
            break;
        }
    }
}catch(FileNotFoundException ex){
    Logger.getLogger(VotingSystem.class.getName()).log(Level.SEVERE, null, ex);
}
koontf.requestFocus();  // you can only call request focus on one element at a time (it does not make sense to call it on all textfields

}                                    

private void baamtfActionPerformed(java.awt.event.ActionEvent evt) {                                       
    // TODO add your handling code here:
}                                 

When I press vote It shoud add up but instead it returns to 1. Any way I can make it add up to the imported value???Below is the code for the Vote Jbutton.

     int pick1 = 0, pick2 = 0,pick3 = 0, pick4 = 0,pick5 = 0, pick6 = 0, pick7 = 0, pick8 = 0, pick9 = 0, pick10 = 0, pick11 = 0, pick12 = 0;
private void voteActionPerformed(java.awt.event.ActionEvent evt) {                                     
    // TODO add your handling code here:

    if (koonchk.isSelected()){
        pick1++;
    koontf.setText(Integer.toString(pick1));
    }
    else if (baamchk.isSelected()){
        pick2++;
    baamtf.setText(Integer.toString(pick2));
    }
    if(sachichk.isSelected()){
        pick3++;
    sachitf.setText(Integer.toString(pick3));
    } 
    else if (fakerchk.isSelected()){
        pick4++;
    fakertf.setText(Integer.toString(pick4));
    }
    if (phonsekalchk.isSelected()){
        pick5++;
    phonsekaltf.setText(Integer.toString(pick5));
    }
    else if (laurechk.isSelected()){
        pick6++;
    lauretf.setText(Integer.toString(pick6));
    }
    if (yeonchk.isSelected()){
        pick7++;
    yeontf.setText(Integer.toString(pick7));
    }
    else if (aguerochk.isSelected()){
        pick8++;
    aguerotf.setText(Integer.toString(pick8));
    }
    else if (agnischk.isSelected()){
        pick9++;
    agnistf.setText(Integer.toString(pick9));
    }
    if (lokichk.isSelected()){
        pick10++;
    lokitf.setText(Integer.toString(pick10));
    }
    else if (lawlietchk.isSelected()){
        pick11++;
    lawliettf.setText(Integer.toString(pick11));
    }
    else if (ryuzakichk.isSelected()){
        pick12++;
    ryuzakitf.setText(Integer.toString(pick12));
    }
}

Upvotes: 1

Views: 36

Answers (1)

Blip
Blip

Reputation: 3171

The problem lies in the fact that you are initialising all the variables as

int pick1 = 0, pick2 = 0,pick3 = 0, pick4 = 0,pick5 = 0, pick6 = 0, pick7 = 0, pick8 = 0, pick9 = 0, pick10 = 0, pick11 = 0, pick12 = 0;

So the previous values of text that you had loaded are lost. I would suggest that you initialise the variables as:

int pick1 = Integer.parseInt(textFields[1].getText());
int pick2 = Integer.parseInt(textFields[2].getText());
.
.
.
//And so on.

This will ensure that the previous number in the text field stays where it is.

A still better solution would be you could declare an Integer array of variables as pick with size of 12. Like:

int[] pick = new int[12];

Then declare a method that is going to update the text fields with the value of pick like:

public void updateTextFields(){
    for(int i = 0; i < 12; i++){
        textFields[i].setText(String.valueOf(pick[i])); //corrected the code
    }
}

Now in your reading file method instead of

    textFields[line++].setText(scanner.nextLine());

use

    pick[i++] = Integer.parseInt(scanner.nextLine());

and at last line of the method simply call the previously declared updateTextFields().

Also in the method to vote, remove all the code for textField.setText(). and in the last line simply call the method upadateTextFields(). What I mean by this is:

private void voteActionPerformed(java.awt.event.ActionEvent evt) {                                     
    // TODO add your handling code here:

    if (koonchk.isSelected()){
        pick1++; //Change to pick[1]++
//    koontf.setText(Integer.toString(pick1)); remove
    }
    else if (baamchk.isSelected()){
        pick2++; //change to pick[2]++
//    baamtf.setText(Integer.toString(pick2)); remove
    }
    if(sachichk.isSelected()){
        pick3++; //change to pick[3]++
//    sachitf.setText(Integer.toString(pick3)); remove
    } 
    else if (fakerchk.isSelected()){
        pick4++; //Change to pick[4]++
//    fakertf.setText(Integer.toString(pick4)); remove
    }
    if (phonsekalchk.isSelected()){
        pick5++; //Change to pick[5]++
//    phonsekaltf.setText(Integer.toString(pick5)); remove
    }
    else if (laurechk.isSelected()){
        pick6++; //Change to pick[6]++
//    lauretf.setText(Integer.toString(pick6)); remove
    }
    if (yeonchk.isSelected()){
        pick7++; //Change to pick[7]++
//    yeontf.setText(Integer.toString(pick7)); remove
    }
    else if (aguerochk.isSelected()){
        pick8++; //Change to pick[8]++
//    aguerotf.setText(Integer.toString(pick8)); remove
    }
    else if (agnischk.isSelected()){
        pick9++; //Change to pick[9]++
    //agnistf.setText(Integer.toString(pick9)); remove
    }
    if (lokichk.isSelected()){
        pick10++; //Change to pick[10]++
    //lokitf.setText(Integer.toString(pick10)); remove
    }
    else if (lawlietchk.isSelected()){
        pick11++; //Change to pick[11]++
    //lawliettf.setText(Integer.toString(pick11)); remove
    }
    else if (ryuzakichk.isSelected()){
        pick12++; //Change to pick[12]++
    //ryuzakitf.setText(Integer.toString(pick12)); remove
    }
    updateTextFields(); //Add this new line.
}

Upvotes: 1

Related Questions