Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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.

share|improve this question
1  
What is your expected output? – anubhava Sep 23 '14 at 7:28
    
i have edited my question, i want the output to be like that. – Muhammad Haryadi Futra Sep 23 '14 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 – talex Sep 23 '14 at 7:34
up vote 2 down vote accepted

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]")));
share|improve this answer
    
This is not what OP wants. Instead of ham.add(line); you need to call line.substring to strip off [ham] and [spam]. – anubhava Sep 23 '14 at 7:33
    
Please give the complete code to do that. thanks. – Muhammad Haryadi Futra Sep 23 '14 at 7:33
    
@anubhava has the right splitting solution. Mine was bad – Daniel Sep 23 '14 at 7:34
    
I have edited after OP clarified – Abhiroop Sarkar Sep 23 '14 at 7:35
    
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()) {...} – Muhammad Haryadi Futra Sep 23 '14 at 7:46

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.