【问题标题】:read a file and determine a String读取文件并确定字符串
【发布时间】:2016-07-03 02:17:02
【问题描述】:

假设我的文本文件包含以下内容:

NOT VOTED/1/gello/18
NOT VOTED/2/tara/24

排列就像status if user already voted or not/voter's number/name/age我把信息线分成数组所以是这样的

info[0]=status/info[1]=voters number/info[2]=name/info[3]=age

这是我的程序的功能:

  1. 读取文件
  2. 让用户输入选民编号
  3. 如果选民人数与 info[1] 匹配,则继续执行步骤 3
  4. 当 info[0] 包含“VOTED”时,它将收到一条错误消息,表明该选民的号码已经投票并输入另一个选民号码。如果为假,它将进入投票过程
  5. 现在 info[0] 将更改为“已投票”

这是我的代码:

        File original = new File("C:\\voters.txt");//open the original file     
        File temporary = new File("C:\\tempvoters.txt");//create temporary file

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

        vNum=JOptionPane.showInputDialog("Enter voters number: ");
        String line=null;
        String something="VOTED";
        while((line=infile.readLine())!=null){

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

                while(info[0].matches(something)) {
                    JOptionPane.showMessageDialog(null, "Voter already voted or Voter not registered. Please try again");
                    vNum=JOptionPane.showInputDialog("Enter voters number: ");
                }

                President();
                Vice_President();
                info[0]="VOTED";
                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();
            }
            else{
                outfile.println(line);
                outfile.flush();
            }


        }

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

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

现在这发生在我制作的代码中:

假设我输入1 作为选民号码。并且因为 info[0] 仍然包含“NOT VOTED”,它将继续进行投票过程,之后 info[0] 的元素现在包含“VOTED”。现在在该过程之后它将返回主菜单。现在,当我再次输入另一个选民号码时,我尝试再次输入1 以查看它是否会出现错误消息。它做了。所以我再次输入了另一个选民的号码2,它的信息[0]仍然包含“未投票”,但错误消息仍然会出现!!我真的被这个过程困住了,我不知道该怎么办了,因为我真的不知道出了什么问题。认为我的程序的这种方法中的功能与我拥有的其他方法相同。请帮帮忙

【问题讨论】:

    标签: java arrays loops text-files file-handling


    【解决方案1】:

    while(info[0].matches(something))

    在我看来,您似乎没有跳出这个 while 循环,因为 info[0] 将始终为真,因为变量“某事”没有被更改。我的建议是:

    {
    JOptionPane.showMessageDialog(null, "选民已投票或选民未注册。请重试");
    vNum=JOptionPane.showInputDialog("输入选民编号:");
    某事=“未投票”; //这将打破循环
    }

    【讨论】:

    • 我尝试将其更改为 info[0].matches("VOTED") 但它仍然是一样的:/你能建议我应该怎么做吗?
    • 编辑了我的帖子以包含建议。
    • 哦,它已经工作了!!!!!!!但仍然存在问题,例如在投票过程后,错误消息会再次出现,让我再次输入选民号码
    • 尝试移动行 String something="VOTED";就在第一个 while 循环的下方,在您将行拆分为数组的上方。
    • 没问题 - 编码愉快!
    猜你喜欢
    • 2012-02-10
    • 2011-03-23
    • 2015-08-28
    • 2018-08-16
    • 2013-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多