【问题标题】:replace a word in text file java替换文本文件java中的一个单词
【发布时间】:2016-06-11 12:13:54
【问题描述】:

我的代码在这里:

public void Edit()throws IOException{
        FileWriter writeFile=new FileWriter("voters.txt", true);
        BufferedWriter outFile=new BufferedWriter(writeFile);
        File readFile=new File("voters.txt");
        BufferedReader read=new BufferedReader(new FileReader(readFile));

        vNumForEdit=JOptionPane.showInputDialog("Enter voters number: ");
        String line=null;
        while((line=read.readLine())!=null){
            String [] info=line.split("/");
            if(info[0].matches(vNumForEdit)){
                String [] forEditMenu={"Name", "Age", "Gender", "Date of Birth", "Place of Birth", "Address", "Civil Status", "Citizenship", "Profession/Occupation", "Father's Name", "Mother's Name"};
                forEdit=(String)JOptionPane.showInputDialog(null, line+"\n\nPlease select what you want to edit", "National Election 2765", 1, null, forEditMenu, forEditMenu[0]);
                switch(forEdit){
                case "Name":
                    newName=JOptionPane.showInputDialog("Enter new name: ");
                    if(info[1].matches(oldName)){
                        outFile.append(info[1]=newName);
                        outFile.close();
                    }
                    read.close();

                    break;
                }
            }
        }
    }

文本文件如下所示

1/d/19/Male/e//s/Single/v/e/e/f
2/c/98/Male//d/c/Single/r/w/q/d

如您所见,我将信息/行设为数组,因此它可以读取索引 0 中的值,即投票数。任务是用户将输入选民编号,然后如果找到,用户将再次输入他想要编辑的内容。我也使用数组来确定姓名、年龄、性别等。数组是这样的:

info[0] = voters number
info[1]= name
info[2] = age
info[3] = date of birth
info[4]= place of birth
info[5] = status
info[6] = citizenship
info[7] = profession
info[8] = father's name
info[9] = mother's name

但我的代码总是以错误 java.lang.NullPointerException 告终。我的大部分变量都是全局声明的

已编辑:这是错误/堆栈跟踪

Exception in thread "main" java.lang.NullPointerException
    at java.util.regex.Pattern.<init>(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at java.util.regex.Pattern.matches(Unknown Source)
    at java.lang.String.matches(Unknown Source)
    at lozada.My_Voting_System_Official.Edit(My_Voting_System_Official.java:135)
    at lozada.My_Voting_System_Official.Menu(My_Voting_System_Official.java:43)
    at lozada.My_Voting_System_Official.main(My_Voting_System_Official.java:15)

【问题讨论】:

  • 请包含堆栈跟踪。
  • 代码的哪一行得到了 NPE ?
  • 另外,使用equalsequalsIgnoreCase 代替matches 来检查它是否是常规字符串。
  • @Veluria 我已经添加了 :)
  • @Sachin 我(想)我添加了哪一行出错了呵呵 :)

标签: java arrays file-handling


【解决方案1】:

我认为你没有声明 oldName 变量 在这段代码中

if(info[1].matches(oldName)){
                        outFile.append(info[1]=newName);
                        outFile.close();
                    }

【讨论】:

  • vNumForEdit 和 oldName 未声明。
  • 我的大部分变量都是全局声明的 :)
  • 好吧,堆栈恍惚状态另有说法。 “未知来源”
  • 在该行下断点,运行代码是调试器,然后看看是否声明了oldName。 ps 使用 .equals
猜你喜欢
  • 1970-01-01
  • 2015-03-02
  • 2015-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-24
  • 2012-11-27
  • 1970-01-01
相关资源
最近更新 更多