0

I have a text file that consist of string. What i want to do is to separate the string with "[ham]" and the string with "[spam]" inside to the different array, how can i do that, i think about to use regex to recognize the pattern (ham & spam), but i have no idea to start. please help me.

String in text file:

good [ham]
very good [ham]
bad [spam]
very bad [spam]
very bad, very bad [spam]

and i want the output to be like this:

Ham array:

good
very good

Spam array:

bad
very bad
very bad, very bad

Help me please.

3
  • 1
    What is your expected output? Commented Sep 23, 2014 at 7:28
  • i have edited my question, i want the output to be like that. Commented Sep 23, 2014 at 7:32
  • 1
    To start you need to read file line by line. Google for it. There are lot of examples in internet. When you meet next problem ask again, But try to be more precise Commented Sep 23, 2014 at 7:34

2 Answers 2

2

Instead of using array I think you should go for ArrayList

List<String> ham=new ArrayList<String>();
List<String> spam=new ArrayList<String>();
if(line.contains("[ham]"))
   ham.add(line.substring(0,line.indexOf("[ham]")));
if(line.contains("[spam]"))
   spam.add(line.substring(0,line.indexOf("[spam]")));
Sign up to request clarification or add additional context in comments.

4 Comments

This is not what OP wants. Instead of ham.add(line); you need to call line.substring to strip off [ham] and [spam].
@anubhava has the right splitting solution. Mine was bad
how to read the string line by line, i use it but it's not working as i need: Scanner terms = new Scanner(new File("training.txt")); while (terms.hasNext()) {...}
0

If you really need do this that way (with regex & array as output) write code like this:

public class StringResolve {

    public static void main(String[] args) {
        try {
            // read data from some source
            URL exampleTxt = StringResolve.class.getClassLoader().getResource("me/markoutte/sandbox/_25989334/example.txt");
            Path path = Paths.get(exampleTxt.toURI());
            List<String> strings = Files.readAllLines(path, Charset.forName("UTF8"));

            // init all my patterns & arrays
            Pattern ham = getPatternFor("ham");
            List<String> hams = new LinkedList<>();

            Pattern spam = getPatternFor("spam");
            List<String> spams = new LinkedList<>();

            // check all of them
            for (String string : strings) {
                Matcher hamMatcher = ham.matcher(string);
                if (hamMatcher.matches()) {
                    // we choose only text without label here
                    hams.add(hamMatcher.group(1));
                }
                Matcher spamMatcher = spam.matcher(string);
                if (spamMatcher.matches()) {
                    // we choose only text without label here
                    spams.add(spamMatcher.group(1));
                }
            }

            // output data through arrays
            String[] hamArray = hams.toArray(new String[hams.size()]);
            System.out.println("Ham array");
            for (String s : hamArray) {
                System.out.println(s);
            }
            System.out.println();

            String[] spamArray = spams.toArray(new String[spams.size()]);
            System.out.println("Spam array");
            for (String s : spamArray) {
                System.out.println(s);
            }

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

    private static Pattern getPatternFor(String label) {
        // Regex pattern for string with same kind: some text [label]
        return Pattern.compile(String.format("(.+?)\\s(\\[%s\\])", label));
    }

}

You can use Paths.get("some/path/to/file") if you need to read it from somewhere in your drive.

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.