Java 方法调用出现两次
我有以下代码/方法:Java 方法调用出现两次,java,methods,file-handling,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 =
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
}
已编辑
例如,我在vNum
中输入1
,然后President()
和second_President()
方法出现一次,效果非常好。在我再次尝试执行此方法之后,我现在在vNum
中输入2
,现在President()
,和副President()
出现两次。这个方法似乎只在第一次执行时有效,因为当我再次尝试执行它时,问题就出现了
以下是我的文本文件的内容(投票状态/投票者编号/姓名/年龄/性别
):
下面是President()
方法的代码(它与second\u President()
方法非常相似):
使用调试器逐步完成执行。您的循环会根据文件中的内容进行迭代,那么您怎么能期望我们在不显示文件内容的情况下知道呢?即使您向我们展示了,您也是在寻求调试帮助,这是离题的。您还应该为第一次和第二次尝试提供相关输入。从代码中,我猜您最初会有一个“投票”行,您的代码正在复制该行。您应该悬停,然后体验两个方法在每次方法调用时被额外调用一次。@VinceEmigh我对使用调试器不太熟悉。我所知道的唯一一件事是控制台中是否会出现错误,而在这段代码中,不会出现任何错误。因此,您应该重点学习如何使用调试器。作为开发人员,它是最有用的工具之一。IDE是内置的。很抱歉,如果我们为你改变规则(即使是这一次),其他人也会希望为他们改变规则。你不该拖延这么久。要想回答这个问题需要付出相当多的努力,看看你怎么连问题涉及的文件都没有包括在内,一开始几乎不可能回答。您可以随时寻找允许您请求调试帮助的站点!我相信还有很多
NOT VOTED/1/faith/18/Male/...
NOT VOTED/2/george/18/Male...
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();
}