从文件读取到会议条件时循环退出

时间:2014-01-20 20:06:33

标签: java file

下面是我正在阅读两列文件的程序。即姓名和第二名是投票。我正在阅读每一行,当输入姓名时,用户符合条件,即如果名称存在于文件中,则提示您已经投票并且无法再次投票。它确实搜索记录但不知何故它不会退出,直到它完成我的文件中的总行数。

此外,我如何计算总票数,即第二列。假设我在该列中有两种类型的条目YES和NO,我如何计算YE的总数和No并比较它们?

感谢您的提前帮助

package samples;

import homework.EmployeeA;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class Votes {

    public static void main(String[] args) {

        File f = new File("E://Employee/votes.txt");

        try {
            Scanner s= new Scanner(f);
                System.out.println("Is motion open for voting?. Press \n1: Yes\n2: No");

            Scanner scan = new Scanner (System.in);
            int choice = scan.nextInt();`enter code here`


    while(s.hasNext())
    {
        String line = s.nextLine();

        String temp[]= line.split(" ");

        EmployeeA e = new EmployeeA("hiren", "yes");

        e.name = temp[0];

        e.vote = temp[1]; 

        ArrayList<EmployeeA> list = new ArrayList<EmployeeA>();

        list.add(e) ;



        for (int i=0; i<list.size(); i++)
        {
            EmployeeA t= list.get(i);




        if (choice==1)
        {   
        System.out.println("Please enter your name to start voting");
        String vname=scan.next();

        if (t.name.equalsIgnoreCase(vname))
        {
System.out.println("opps you have already voted once and you are not eligible to vote again");
        }
        else
        {
            System.out.println("Hey greate news");
        }
        }   

if (choice==2)
{

    {
        System.out.println(" Please have patience till Motion is opened for voting");
    }


}   





    StringBuffer sb = new StringBuffer("D://Employee/votes.txt");
    try{
        FileWriter fwriter = new FileWriter(f);
        BufferedWriter bwriter = new BufferedWriter(fwriter);
        bwriter.write(sb.toString());
        bwriter.close();
     }
    catch (IOException e1){
          e1.printStackTrace();
*/


    }

    }

    if (choice==2)
    {

        {
    System.out.println(" Please have patience till Motion is opened for voting");
        }


    }
    } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



    }

}

1 个答案:

答案 0 :(得分:0)

要在名称与现有条目匹配时退出读取文件,请使用“break”语句。在java中,break语句用于退出for循环(在这种情况下你需要它),并且因为文件中的条目是从循环内部读取的,所以它会退出读取文件。因此,请将此部分代码修改为:

    if (t.name.equalsIgnoreCase(vname))
    {
        System.out.println("opps you have already voted once and you are not eligible to vote again");
        break; // This statement will exit the terminate the loop immediately
    }

由于您没有提及计算投票数的代码部分,我可以为您提供一个基本的代码算法,您应该能够轻松实现。

    String line; // make this string hold each line in the file
    int yes = 0, no = 0; 
    Scanner s = new Scanner(f) // Start reading from the top of the file
    while (s.hasNext()) {
        line = s.nextLine();
        String[] values = line.split(" ");
        String vote = values[1] // Assuming it is name followed by vote
        if (vote.equalsIgnoreCase("No")) // Or you can directly use values[1].equalsIgnoreCase("No")
            yes++;
        else
            no++;
    }
    // Now yes holds count of "Yes" votes and no holds count of "No" votes