【问题标题】:read if it matches/contains the voters number in text file读取它是否匹配/包含文本文件中的选民编号
【发布时间】:2016-03-14 12:24:32
【问题描述】:

例如,我的文本文件包含以下内容:

1/hannah/18
2/jeorge/20

安排就像votersnumber/name/age 现在我希望我的程序读取文件,确定文本文件中的votersnumber 是否包含用户输入的选民号码,然后执行以下程序。如果文件不包含/不匹配用户输入的内容,程序将让用户再次输入选民号码。

我尝试通过以下代码做到这一点:

File Orig_outFile = new File("C:\\Users/Regz-pc/workspace/lozada/voters.txt");
    BufferedReader infile = new BufferedReader(new FileReader(Orig_outFile));

    String voters[]=new String[10+1]; //purpose of this is to determine whether the voter already voted. the [10+1] is not final but just a sample/just for test. but i will still be using this
    String allVoters="";
    for(int index=1;index<voters.length;index++){
        voters[index]="NOT VOTED YET";
        allVoters=allVoters+"voter["+index+"]="+voters[index]+"\n";
        }

    vNum2=Integer.parseInt(JOptionPane.showInputDialog("Enter voters number: "));//user will enter the voters number
    vNum=Integer.toString(vNum2);
    String line=null;

    while((line=infile.readLine())!=null){
        String [] info=line.split("/");//divide the votersnumber/name/age
        if(!info[0].contains(vNum) && voters[vNum2].contains("VOTER ALREADY VOTED")){//determine if the vNum2 exist in text file
                JOptionPane.showMessageDialog(null, "Voter already voted or Voter not registered. Please try again");
                vNum2=Integer.parseInt(JOptionPane.showInputDialog("Enter voters number: "));
                }
        else{
            voters[vNum2]="VOTER ALREADY VOTED";
            JOptionPane.showMessageDialog(null, allVoters);

            President();
        }
    }
    infile.close();

但此代码只会执行一次,但执行后会出现错误消息Voter already voted or Voter not registered. Please try again,然后让用户再次输入选民号码,然后返回主菜单。当我再次尝试执行该程序时,它只会让我输入选民号码,然后返回主菜单。

【问题讨论】:

    标签: java text file-handling


    【解决方案1】:

    这不是有效的java条件,

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

    改用这个:

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

    并阅读该行:

    String line= "";
    while(infile.readLine() !=null){
        // read the line
        line=infile.readLine()
    
        // res of your code...
    }
    

    【讨论】:

    • 哦,这可以解决我关于 President() 只执行一次的问题,现在它可以工作了,非常感谢 :) 但你知道我应该怎么做才能确定输入的选民人数是否存在于文本文本文件?
    • @bruh 我不明白你的意思我应该怎么做才能确定输入的选民人数是否存在于文本文件中?
    【解决方案2】:
    if(!info[0].contains(vNum) && voters[vNum2].contains("VOTER ALREADY VOTED")){//determine if the vNum2 exist in text file
        JOptionPane.showMessageDialog(null, "Voter already voted or Voter not registered. Please try again");
        vNum2=Integer.parseInt(JOptionPane.showInputDialog("Enter voters number: "));
    }
    

    在这里,我认为您也需要更改 vNum。

    【讨论】:

    • 嗯,没有什么真正改变:/但你能建议任何关于如何编辑 vNum 的建议吗?
    • 我刚刚注意到,您在 if 语句中使用了这两个变量,所以如果您要更改 vNum2,您也应该更改 vNum,例如:vNum=Integer.toString(vNum2 );
    • 在while循环里面?
    • 哦,我现在确实将我的 vNum=Integer.toString(vNum2) 移动到了 while 循环中。但它仍然没有发生任何事情:/我可以使用while而不是if-else tho吗?
    • 如果您使用一段时间,您将一遍又一遍地输入选民编号(对于文件中的每一行),而最后您将输入选民的确切编号。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    相关资源
    最近更新 更多