I'm working with a large text file that has thousands of lines of text that contain some information. Sometimes the software will randomly add a character in place which will fail our upload. I'm trying to create a program that finds a string that is hard coded and then searches within the line and replaces or removes the invalided character. Here is some of the contents of the text file.
MTR17000386001000000000000000RA 124359
00010000004000000040000000 000NN NNE 000 N
RDG17000386
KWHL000000000R00000100000059534000405162019075929N000400000010N8486
000010500R 00000010010000059226
RFF1700038652126007 ERT
0000000952.0062500070014051620190759290005953476Type 7 0000N 6
MTR17000386001000000000000000RA 114818
00010000005000000050000000 000NN NNE 000 N
RDG17000386
DMDL000000000R000001000.0072666035305162019112344N000100000010N8486
005180500R 00000010010000072666
RFF1700038611861733 ERT
0000000952.0062500070000051620191123440007266680Type 7 0000N 6
On the line of RDG17000386 DMD you can see that there is a period. The period is not supposed to be there and needs to be replaced or removed from the file.
In my current code I"m searching each line that start with "RDG" and it works find, but I want to limit the search with only the lines that include "DMD" within the line and I've tried changing the RDG to DMD, which didn't work because the line doesn't start with DMD. I'm not entirely sure how to remove or replace the period from the file. Here is what my code looks like so far.
import java.io.*;
public class ReadLineAndReplace {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("FilePath"));
StringBuilder sb = new StringBuilder();
String line = "";
while ((line = br.readLine()) != null) {
if (line.startsWith("RDG")) {
//assuming the replace would be inserted here
System.out.println(line);
}
}
}
}
Use. matches()
with a regular expression
line.matches("^RDG.*DMD.*")
to replace the dot with a zero
if (line.matches("^RDG.*DMD.*")) {
line = line.replace('.', '0')
}