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.

2 Answers2

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]")));
Abhiroop Sarkar
  • 2,251
  • 1
  • 26
  • 45
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.