Hi everyone,

i would like to extract data from text like the following one:

...........
2011/01/12 The thread 001 title should clearly describe 
2011/01/12 This describe or discussion topic. General
2011/01/12 This desc  002 nor discussion topic. General 
2011/01/12 titles is  003 as 'Help Me' or 'Urgent' 
2011/01/12 This describe or discussion topic. General 
...........etc

I want to extract only lines with number like 001 or 002 , every line with a number
like in the example but not lines without number.

The text content 500 lines but some of them don't contain number.
in Java what method or function could i use to do my prog?

i know how to read specific line from text with the lineNumber but here it's different.

any help please.
Thank you.

Recommended Answers

All 8 Replies

Try creating two scanners. Use one to read the file and one to find which lines to read. So you would use a regular expression to find which lines contain the number in the one scanner then use the other scanner to read that line. Hope this helps.

Ok for the first part for reading the text file but for the second part do i use something like:

....
Scanner scanner = new Scanner(file);
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
if (line.equals("001");//checking lines with 001
Printwriter(line);//prints the the line containing 001
...

End i tried another example but it's not working why?
i follow your tips.

The goal is to get lines which contains 001 and 002.
here is the code:

Scanner scanner = null;
         StringBuilder paragraph = new StringBuilder();

              try {
                       scanner = new Scanner(new File("C:/Source/Source.txt"));
                                 Scanner.FindlnLine("001"&&"002");

              long db = System.currentTimeMillis();
              int nmbreLigne =0;
              while (scanner.hasNextLine())
              {
                  nmbreLigne++;
                String line = scanner.nextLine();
                paragraph.append(line);
                paragraph.append("\r\n");

                }

              long df = System.currentTimeMillis();
              System.out.println("Nombre de ligne "+nmbreLigne+" en "+(df-db)/1000+" s");
              }
                catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                finally
                {
                    if(scanner != null)
                    {
                        scanner.close();
                    }
                }
                BufferedWriter bw = null;
                try
                {
                  bw=new BufferedWriter(new FileWriter(new File("C:/Target/Target.txt"),true));
                    bw.write(paragraph.toString());
                    bw.newLine();
                    bw.close();

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    {
                        if(bw != null)
                        {
                            scanner.close();
                        }
                    }

In order for this to work you need to count the number of lines while searching for a line that contains a number. Then use the line count with the other scanner to get that line and read it like this.

Scanner input = new Scanner(file);
Boolean lines[] = new Boolean[500];

for(int i = 0; i < 500; i++){
    String line = input.nextLine();
    int index001 = line.indexOf("001");
    int index002 = line.indexOf("002");
    if(index001 >= 0)
        lines[i] = true;
    else if(index002 >= 0)
        lines[i] = true;
    else
        lines[i] = false;
}

input.close();

Scanner read = new Scanner(file);
for(int i = 0; i < 500; i++){
    String line = read.nextLine();
    if(lines[i]){
        System.out.println(line);
}

read.close();

that should do it if there is anything you don't understand in my post please ask I will explain it.

your line.equals statement will never evaluate to true because the line will never equal only 001.

Thank you for your example, i tried to write your code inside another code but,
the result is :
it only reads all line of my text and it doesn't create the outputfile Target-test.txt

I don't understand at when in my code it reads all the line.

Here's my code:

package scannerreadfile;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class ScannerReadFile
{
    private static BufferedWriter bw;
    public static void main(String[] args) throws FileNotFoundException, IOException {

        Scanner scanner = null;
        StringBuilder paragraph = new StringBuilder();
        FileReader fr = new FileReader("C://Source/Source.txt");

        try
        {

Scanner input = new Scanner(fr);
Boolean lines[] = new Boolean[500];

for(int i = 0; i < 500; i++){
    String line = input.nextLine();
    int index001 = line.indexOf("1");
    int index002 = line.indexOf("2");
    if(index001 >= 0)
        lines[i] = true;
    else if(index002 >= 0)
        lines[i] = true;
    else
        lines[i] = false;
}

input.close();

Scanner read = new Scanner(fr);
for(int i = 0; i < 500; i++){
    String line = read.nextLine();
    if(lines[i]){
        System.out.println(line);
}

read.close();
bw=new BufferedWriter(new FileWriter(new File("C:/Target/Target-test.txt"),true));


bw.write(paragraph.toString());
bw.newLine();
bw.close();

}
catch (IOException e)
{
e.printStackTrace();
}

if(bw != null)

scanner.close();
}
}

If you want it to print to the file simply tell it to do that instead of having it print to the command window in line.

Start by moving line 42 to right after 43.

Then in line 44 have it print to the file instead of the command line or add another line inside that if loop that makes it print to the file.

When you said it was reading all of the lines did you mean it was printing all of them?

one more thing change the condition in the if loop to:

lines[i] == true

OK, you are right i forgot to remove the line44 with command line.
i shift the line 48 to 44 because i want to put data in a file.

When i execute the code, it says :
that it didn't find the line:

Exception in thread "main" java.util.NoSuchElementException: No line found
        at java.util.Scanner.nextLine(Scanner.java:1516)
        at scannerreadfile.ScannerReadFile.main(ScannerReadFile.java:45)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second

)

I think what i want to do it's impossible because in the line i want to extract there are already other numbre like "1"
ex: 2011/01/12 The thread 001 title should clearly describe

so i think i have to find another way .
maybe with BufferedReader

thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.