简体   繁体   中英

replace a word in text file java

i have my code here:

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;
                }
            }
        }
    }

the text file looks like this

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

as you can see i made the info/line as array so it can read the value in index 0 which is the voting number. the task is the user will input the voters number and then if it's found, the user will enter again what does he want to edit. i use array as well to determine the name, age, gender, etc. the array is like this:

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

but my code always end up with error java.lang.NullPointerException . most of my variables there are declared globally

EDITED: here's the error/stack trace

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)

i think you didn't declare the oldName Variable in this code

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM