tags:

views:

58

answers:

2

I would like to find out how to print a a certain string from an input file using a scanner. For the string to be printed the line must start with *star and the string must be surrounded by quotation marks, and be the next token and on the same line as *star ignoring white space of course.

Sample input text file: "test.txt"

this is a test

*star "variableX"

more testing

*star "variableY

much more

testing

*star next"variableZ"

Based on this sample input text the output should be only.

"variableX"

Here is part of my code:

    Scanner scanner = new Scanner (new File ("test.txt"));

    while (scanner.hasNextLine()){

        if(scanner.hasNext("*star")) {
            scanner.next();

            if(scanner.hasNext()){
                String test = scanner.next();
                System.out.println(test);
            }

But it's missing some key things. Help is much appreciated!

A: 

This does the trick

public static void main(String[] args) throws FileNotFoundException
    {
        Scanner scanner = new Scanner (new File ("c:\\test.txt"));

        while (scanner.hasNextLine()){
            if(scanner.hasNext(Pattern.compile("\\*star"))) {
                scanner.next();
                System.out.println(scanner.nextLine());
                return;
            }
            else
                scanner.nextLine();
        }
    }

Edit: The quotation part was missing in the answer, did not remember to put it as I got the output even without it, but here it is a more robust solution:

Scanner scanner = new Scanner (new File ("c:\\new.txt"));

        while (scanner.hasNextLine()){
            if(scanner.hasNext(Pattern.compile("\\*star"))) {
                scanner.next();
                String s = scanner.nextLine();
                Matcher m = Pattern.compile("\".+\"").matcher(s);
                if(m.find())
                {
                    System.out.println(m.group());
                    return;
                }
            }
            else
                scanner.nextLine();
        }
Gaurav Saxena
This one fails to strip the leading *star and "" quotes, as in the example output...
andersoj
did you really try to run it on the input? It sure got the right answer for me even without the quotation code I have just added; but I agree it should have been there. The previous code sure stripped of the leading *star(when a line started with it, as mentioned) and quotations need not be stripped, as shown in the output.
Gaurav Saxena
+2  A: 
package so3947761;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
  private static final Pattern RELEVANT_LINE_PATTERN =
      Pattern.compile("^\\*star\\s+\"(.*)\"$");

  public static void main(String[] args) throws FileNotFoundException {
    Scanner scanner = new Scanner(new File("test.txt"));

    while (scanner.hasNextLine()) {
      String line = scanner.nextLine();

      Matcher m = RELEVANT_LINE_PATTERN.matcher(line);
      if (m.matches()) {
        String data = m.group(1);

        System.out.println(data);
      }
    }
  }
}

Some remarks:

  • I'm using the Scanner only for splitting the input into lines.
  • The real work is done by the regular expression pattern, which contains all the requirements that you stated.
  • If you want to parse the text in quotes further (for example to replace \n with a newline), this cannot be done by the regular expression, so you have to explicitly define which sequences to replace.
  • You may want to add \\s* at the right of the ^ (beginning of the line) and at the left of the $ (end of the line) if you want to allow whitespace at these places.
Roland Illig
That regex seem to be good but did not work for me for the input given in the question
Gaurav Saxena
That's weird because I tested it with exactly the input you gave. Well, almost, I removed the leading white-space. So you should probably follow the advice in the fourth bullet point above.
Roland Illig