3

I don't know how to find these words.. example I have this text...

The other day I went to the <location> and bought some <plural-noun> . Afterwards, I went to <location> , but it was very <adjective> so I left quickly and went to <location> .

I don't know what to search cause when I search < and > on google, it will be ignored. Need help how to get this string.

so I will get the <location>, <plural-noun>, <location>, <adjective>, <location>

I must use the charAt() method. My attempt:

String string = this.fileName;
for(int i = 0; i < string.length(); i++)
                if((string.charAt(i) == '<') && (string.charAt(i) == '>'))
                    System.println(""); //<-------- IM STUCK HERE

I don't know... almost two days no sleep.

My current but one last problem... How to remove < and > on each word on display?

String string = this.template;
        Pattern pattern = Pattern.compile("<.*?>");
        Matcher matcher = pattern.matcher(string);

        List<String> listMatches = new ArrayList<String>();

        while(matcher.find()) {
            listMatches.add(matcher.group());
        }
        // System.out.println(listMatches.size());
        int indexNumber = 1;
         for(String s : listMatches) {
             System.out.println(Integer.toString(indexNumber) + ". " + s);
             indexNumber++;
         }
Brad
  • 2,035
  • 2
  • 19
  • 32
Storm Spirit
  • 1,326
  • 4
  • 16
  • 32

3 Answers3

5

You can use Pattern and Matcher classes.

  1. Search for regex Pattern <.*?>.
  2. Find the pattern with Matcher.
hata
  • 8,429
  • 5
  • 32
  • 57
2

There are really two questions here, so I'll answer only the last one; when you have the <text> that you want, go like this:

String text = "<the_text_you_want>";

text.replace("<","").replace(">","").replace("-"," ");

That will get rid of the delimiters.

Brad
  • 2,035
  • 2
  • 19
  • 32
  • thanks, do you know how to change `plural-noun` into `plural noun` ? – Storm Spirit Oct 04 '15 at 07:15
  • 2
    `Thanks for the feedback! Once you earn a total of 15 reputation, your votes will change the publicly displayed post score.` can't upvote sorry dude... I will upvote if I reach 15 – Storm Spirit Oct 04 '15 at 07:32
1

Read the whole line and store it in, say String line. Then, use:

String line = "The other day I went to the <location> and bought some <plural-noun> . Afterwards, I went to <location> , but it was very <adjective> so I left quickly and went to <location> ."; 

boolean found = false;
String data[] = new String[20];
int counter = 0;

Arrays.fill(data, "");

for(int i = 0; i < line.length() && counter < 20; i++) {
    if(line.charAt(i) == '<')
        found = true;
    else if(line.charAt(i) == '>' && found) {
        found = false;
        counter++;
    }
    else if(found) {
        data[counter] += line.charAt(i);
    }
}

for(int i = 0; i < counter; i++)
    System.out.println("Scanned data #" + (i + 1) + " = " + data[i]);
Spikatrix
  • 19,378
  • 7
  • 34
  • 77