-1
  public class event implements ActionListener { //_________________________________

    public void actionPerformed(ActionEvent Action){
      System.out.println("Clicked");

      String searchObject = tagTextField.getText();
   FileReader fr = new FileReader(searchObject + ".txt");
    BufferedReader br = new BufferedReader(fr);

// if(line.startsWith("001")){

// }

That's what I have right now. My goal is that a text file has either a 001 or a 002 or 0003 as their first word. Then, following the word is a letter. I want to be able to identify all the letters that are in a line that start with 001, they all are merged into one variable to be displayed in a JTextField. An example:

001 A

002 B

001 C

001 Z

005 A

002 C

My hope is that the program will return "A C Z". Sadly I asked this question already and got a really good answer, but I decided to not do it the first way and asked if he can implement some HTML in there. Now though, I no longer need HTML but I cannot retrieve his old code that he suggested I used (that would of been perfect for now).

Background: SearchObject is the name of the file.

the last two lines are //'ed out because line isn't definined and I have no idea how to start it. Any help would be very appreciated.

This isn't my full code but the rest of my program I believe is irrelevant because the rest doesn't not deal with file reading or anything.

Nic Cal
  • 5
  • 4
  • 1
    What exactly are you stuck on? Reading a file? Grouping lines by a specific token? – Jacob G. Mar 03 '19 at 19:32
  • 1
    @JacobG. I have no idea on how to make a file reader reach each line, and then if the line starts with 001, copy that line, and continue reading until it reaches another line that starts with 001, in which it'll also copy that line, merging the two lines together into one variable until its finished reading the file. – Nic Cal Mar 03 '19 at 19:34
  • 2
    I recommend you split up your problem into multiple, smaller problems. First, focus on how to read a file using Java: https://stackoverflow.com/questions/4716503/reading-a-plain-text-file-in-java – Jacob G. Mar 03 '19 at 19:35
  • 1
    @JacobG. Thank you for the suggestion but I do know how to read a file using FileReader but I forgot to include it in the code. I just don't know how to set it up to where if the line starts with 001, save that line, continue, and merage all lines that start with 001 into one variable so I can display it. – Nic Cal Mar 03 '19 at 19:37

2 Answers2

0

You can use regex

BufferedReader br = new BufferedReader(file);
String line;
String result;

while(br.readLine() != null) {
    line = br.readLine();
    if(line.matches("^001.*$")) {
        result += line.subString(4, 5) + " ";
    }
}

Until the line read isn't equal to null (the end), then assigns the line read to the string line.

^ stands for line start, 001 is the plain text you want to match, .* is any character (.) contained zero or more times (*) and $ is the line end.

Then if that is true, adds a sub string of the line which starts from the 4th char and ends in 5th plus a space.

  • 1
    Thanks, but the part where it says //Do Stuff is what Im asking for help on. The action I want it to do is to copy that line, and every other line that starts with 001. Like the example I put in my post, A,C,Z had 001 next to it so the output was "A C Z". Thanks for the suggestion though! – Nic Cal Mar 03 '19 at 19:41
  • 1
    Thanks, but I don't have line defined in my program, only reason its in my post was because I was playing around and wanted to see what line should be defined to. What should line equal to make your code work? – Nic Cal Mar 03 '19 at 19:53
  • @Nic Cal I edited once again, is this what you meant? –  Mar 03 '19 at 20:11
  • 1
    It seems to only output the last 001 sentence and not combine them. I wont take up anymore of your time, Deadpool managed to get one that is compatible with mine code. Thank you very much anyways! – Nic Cal Mar 03 '19 at 20:36
0

Try using Files from java-8 read all the lines from file, and filter lines startsWith 001. Then split string get the values from index 1

try (Stream<String> stream = Files.lines(Paths.get(searchObject + ".txt"))) {

        List<String> abc = stream.filter(str->str.startsWith("001"))
                                 .map(s->s.split("001")[1]).collect(Collectors.toList());

        System.out.println(abc);    // A C Z

    } catch (IOException e) {
        e.printStackTrace();
    }
Deadpool
  • 29,532
  • 9
  • 38
  • 73
  • 1
    Oh man that looks really complex you're gonna have to help me out on this. I have java.nio.file.Files; imported, but the complier cannt find the symbol Stream, it says that List cannot take parameters, and it cannot find the symbol startsWith nor Collectors. Any idea on what im doing wrong? – Nic Cal Mar 03 '19 at 19:48
  • you should add these imports ```import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream;``` @NicCal and check my updated answer – Deadpool Mar 03 '19 at 19:53
  • 1
    Thanks so much, its working good. But it only seems to copy the letter next to the 001. I should have put it in my post but some lines contains spaces and other letters. I.E "0001 A X Z A" But instead I get only A. Thanks so much for the solution though! – Nic Cal Mar 03 '19 at 20:00
  • so you need everything after `001` @NicCal? – Deadpool Mar 03 '19 at 20:02
  • 1
    Yes I do, Im sorry I probably should of said it in the post that the lines do contain other letters and spaces. – Nic Cal Mar 03 '19 at 20:03
  • so simple do this `s.split("001")[1]` this will work if string have `001` only once at the @NicCal – Deadpool Mar 03 '19 at 20:06