2

I am trying to create a method that parses a text file and returns a string that is the url after the colon. The text file looks as follow (it is for a bot):

keyword:url
keyword,keyword:url

so each line consists of a keyword and a url, or multiple keywords and a url.

could anyone give me a bit of direction as to how to do this? Thank you.

I believe I need to use a scanner but couldn't find anything on anyone wanting to do anything similar to me.

Thank you.

edit: my attempt using suggestions below. doesn't quite work. Any help would be appreciated.

    public static void main(String[] args) throws IOException {
    String sCurrentLine = "";
    String key = "hello";

    BufferedReader reader = new BufferedReader(
            new FileReader(("sites.txt")));
    Scanner s = new Scanner(sCurrentLine);
    while ((sCurrentLine = reader.readLine()) != null) {
        System.out.println(sCurrentLine);
        if(sCurrentLine.contains(key)){
            System.out.println(s.findInLine("http"));
        }
    }
}

output:

    hello,there:http://www.facebook.com
null
whats,up:http:/google.com

sites.txt:

   hello,there:http://www.facebook.com
whats,up:http:/google.com
2
  • 2
    Have you looked at the Scanner documentation yet? Commented Aug 29, 2013 at 7:56
  • Use a BufferedReader to get the lines of the file and then you can use a Scanner or split or, probably easiest, regex to tokenise the line. Commented Aug 29, 2013 at 7:58

4 Answers 4

2

You should read the file line by line with a BufferedReader as you are doing, I would the recommend parsing the file using regex.

The pattern

(?<=:)http://[^\\s]++

Will do the trick, this pattern says:

  • http://
  • followed by any number of non-space characters (more than one) [^\\s]++
  • and preceded by a colon (?<=:)

Here is a simple example using a String to proxy your file:

public static void main(String[] args) throws Exception {
    final String file = "hello,there:http://www.facebook.com\n"
            + "whats,up:http://google.com";
    final Pattern pattern = Pattern.compile("(?<=:)http://[^\\s]++");
    final Matcher m = pattern.matcher("");
    try (final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(file.getBytes("UTF-8"))))) {
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            m.reset(line);
            while (m.find()) {
                System.out.println(m.group());
            }
        }
    }
}

Output:

http://www.facebook.com
http://google.com
Sign up to request clarification or add additional context in comments.

Comments

0

Use BufferedReader, for text parsing you can use regular expresions.

Comments

0

You should use the split method:

String strCollection[] = yourScannedStr.Split(":", 2);
String extractedUrl = strCollection[1];

Comments

-1

Reading a .txt file using Scanner class in Java

http://www.tutorialspoint.com/java/java_string_substring.htm

That should help you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.