方法调用出现两次

时间:2016-03-18 06:27:49

标签: java methods file-handling

我有这个代码/方法:

public void Vote() throws IOException{
        File Orig_outFile = new File("C:\\voters.txt");//open the original file     
        File Temp_outFile = new File("C:\\tempvoters.txt");//create temporary file

        BufferedReader infile = new BufferedReader(new FileReader(Orig_outFile));//read the file
        PrintWriter outfile = new PrintWriter(new PrintWriter(Temp_outFile));//write the data

        vNum=JOptionPane.showInputDialog("Enter voters number: ");
        String line=null;

        while((line=infile.readLine())!=null){

            String something="VOTED";
            String [] info=line.split("/");
            if(info[1].matches(vNum)){

                while(info[0].matches(something)) {
                    JOptionPane.showMessageDialog(null, "Voter already voted. Please try again");
                    vNum=JOptionPane.showInputDialog("Enter voters number: ");
                    something="NOT VOTED";
                }
                info[0]="VOTED";
                President();
                Vice_President();
            }
            all=info[0]+"/"+info[1]+"/"+info[2]+"/"+info[3]+"/"+info[4]+"/"+info[5]+"/"+info[6]+"/"+info[7]+"/"+info[8]+"/"+info[9]+"/"+info[10]+"/"+info[11]+"/"+info[12];
            outfile.println(all);
            outfile.flush();
        }

        infile.close();
        outfile.close();

        Orig_outFile.delete();//delete the original file
        Temp_outFile.renameTo(Orig_outFile);//rename the temporary file to original file

    }

EDITED

例如,我在1之后输入vNum,之后President()Vice_President()方法出现一次并完美无缺。之后我尝试再次执行此方法,我现在在2输入vNum,现在输入President()Vice_President() 出现两次。看起来这种方法只能在第一次执行时起作用,因为当我再次尝试执行它时,现在出现了问题

以下是我的文字文件(voting status/voters number/name/age/gender)的内容:

NOT VOTED/1/faith/18/Male/...
NOT VOTED/2/george/18/Male...

这里是President()方法的代码(它与Vice_President()方法非常相似):

public void President() throws IOException{
        File original = new File("C:\\president_voting_score.txt");//open the original file
        PrintWriter outfile = new PrintWriter(new FileWriter(original, true));//write the data

        JOptionPane.showMessageDialog(null, "WELCOME!\n\n"
                + "Please be guided that you can only vote once.\n"
                + "You cannot repeat the voting process after you click 'OK'.\n"
                + "So please choose carefully.\n"
                + "Click 'OKAY' in this message if you're ready to vote now.\n\n"
                + "THANK YOU AND VOTE WISELY!");
        votepres=Integer.parseInt(JOptionPane.showInputDialog("YOU'LL BE NOW VOTING FOR YOUR PRESIDENT:\n"
                + "Enter 1 for "+pres1+"\n"
                + "Enter 2 for "+pres2+"\n"
                + "Enter 3 for "+pres3+"\n"
                + "Enter 4 for "+pres4));

        while(votepres>4){
            JOptionPane.showMessageDialog(null, "Invalid input. Please try again");
            votepres=Integer.parseInt(JOptionPane.showInputDialog("YOU'LL BE NOW VOTING FOR YOUR PRESIDENT:\n"
                    + "Enter 1 for "+pres1+"\n"
                    + "Enter 2 for "+pres2+"\n"
                    + "Enter 3 for "+pres3+"\n"
                    + "Enter 4 for "+pres4));
        }

        if (votepres==1){
            int[] incrementVote={1};
            for (int v : incrementVote){
                result1 = (pvotes[v - 1]+=1);
                }
            outfile.println("1="+pres1+"="+result1);
            }
        else if (votepres==2){
            int[] incrementVote={2};
            for (int v : incrementVote){
                result2 = (pvotes[v - 1]+=1);
                }
            outfile.println("2="+pres2+"="+result2);
            }
        else if (votepres==3){
            int[] incrementVote={3};
            for (int v : incrementVote){
                result3 = (pvotes[v - 1]+=1);
                }
            outfile.println("3="+pres3+"="+result3);
            }
        else if (votepres==4){
            int[] incrementVote={4};
            for (int v : incrementVote){
                result4 = (pvotes[v - 1]+=1);
                }
            outfile.println("4="+pres4+"="+result4);
            }
            outfile.close();

    }

0 个答案:

没有答案