1

so i have this code for my voting system

public void Register() throws IOException{
    String [] menuGender={"Male", "Female"};
    String [] menuStatus={"Single", "Married", "Widow(er)", "Legally separated"};


    do{
    FileWriter writeFile=new FileWriter("voters.txt", true);
    BufferedWriter outFile=new BufferedWriter(writeFile);

    age=Integer.parseInt(JOptionPane.showInputDialog("Age: "));
    while(age<18){
        JOptionPane.showMessageDialog(null, "Voter should be 18 or above");
        age=Integer.parseInt(JOptionPane.showInputDialog("Age: "));
    }
    name=JOptionPane.showInputDialog("Full Name: ");
    gender=(String)JOptionPane.showInputDialog(null, "Gender:", "Election 2765", 1, null, menuGender, menuGender[0]);
    if(gender=="Male"){
        gender="Male";
    }
    else{
        gender="Female";
    }
    dBirth=JOptionPane.showInputDialog("Date of Birth: ");
    pBirth=JOptionPane.showInputDialog("Place of Birth: ");
    address=JOptionPane.showInputDialog("Address\n(Province, City/Municipality, Barangay, House No./Street: ");
    status=(String)JOptionPane.showInputDialog(null, "Civil Status:", "Election 2765", 1, null, menuStatus, menuStatus[0]);
    if(status=="Single"){
        status="Single";
    }
    else if(status=="Married"){
        spouse=JOptionPane.showInputDialog("Spouse Name: ");
        status="Married(Spouse: "+spouse+")";
    }
    else if(status=="Widow(er)"){
        status="Widow(er)";
    }
    else{
        status="Legally Separated";
    }
    citizenship=JOptionPane.showInputDialog("Citizenship:");
    job=JOptionPane.showInputDialog("Profession/Occupation: ");
    tin=JOptionPane.showInputDialog("Tin Number: ");
    father=JOptionPane.showInputDialog("Father's Full Name: ");
    mother=JOptionPane.showInputDialog("Mother's Full Name: ");
    votersNumber++; 

    vNumber=Integer.toString(votersNumber);

    outFile.append(vNumber+"/"+name+"/"+age+"/"+gender+"/"+dBirth+"/"+pBirth+"/"+address+"/"+status+"/"+citizenship+"/"+job+"/"+father+"/"+mother);
    outFile.newLine();

    outFile.close();

    selectYN=JOptionPane.showInputDialog("Continue?\n[1]Yes [2]No");
    }while(selectYN!="2");
}

my problem is my do while loop won't work. every time i type 2 it will still go back to the enter informations. my goal is when i type 2, it will go back to it's menu. this is the code of the menu

String choice, choice2; String [] firstMenu = {"Register/Edit Information", "Voters List", "Voters Validation and Candidate Voting", "Exit"};

    do{
        choice=(String)JOptionPane.showInputDialog(null, "Welcome! Please choose:", "National Election 2765", 1, null, firstMenu, firstMenu[0]);
    switch(choice){
    case "Register/Edit Information":
        String [] secondMenu = {"Register", "Edit", "Delete", "Back"};

        do{
            choice2=(String)JOptionPane.showInputDialog(null, "Please choose:", "Election 2765", 1, null, secondMenu, secondMenu[0]);
            switch(choice2){
            case "Register":
                Register();
                break;
            case "Edit":
                break;
            case "Delete":
                break;
            }
        }while(choice2!="Back");

        break;
    case "Voters List":

        break;
    case "Voters Validation and Candidate Voting":

        break;
    }
    }while(choice!="Exit");

my do while loop here works, tho. only the Register method won't work.

bruh
  • 79
  • 3
  • 13

2 Answers2

2

You cannot compare Strings with == (that means != is also wrong).

Use equals instead.

Replace selectYN!="2" by !"2".equals(selectYN) (take note that there is a ! before to negate the result)

Burkhard
  • 14,596
  • 22
  • 87
  • 108
  • oh thank you so much it works!! but why does the first menu work? i mean it is also a String. and i did this before and it worked. i even thought the reason why it won't work is i used BufferedWriter – bruh Feb 27 '16 at 08:40
0

Insert line:

    alert(selectYN);

Just before last while to be sure of the values you are getting.

Arif Burhan
  • 507
  • 4
  • 12