-1

I wanted to take a string such as

[item.a.1234] is very close to [item.a.2345]

and print out only the "item" to the screen...how might I do that.

So far I have:

import java.io.*;
public class Solution {
    public static void main(String args[] ) throws Exception {
        InputStreamReader istream = new InputStreamReader(System.in);
        BufferedReader bReader = new BufferedReader(istream);
        String output = bReader.readLine();
        while (output != null){
            System.out.println(output);
            output = bReader.readLine();
        }
    }
}

But this gives me:

[item.a.1234] is very close to [item.a.2345]

I want:

item.a.1234,item.a.2345

without the word in between the "Items"...any suggestions?

koala421
  • 786
  • 3
  • 11
  • 27
  • *"...any suggestions?"* For better help sooner, post an [SSCCE](http://sscce.org/) that actually attempts it! Given the `String` can be hard-coded, it seems that source above is almost entirely unrelated. – Andrew Thompson Dec 15 '13 at 17:46
  • seperate your strings using String manipulation methods. I think the openening and closing brackets are nice indicators for the extraction of your items – Diversity Dec 15 '13 at 17:50
  • 2
    And how is this related to "generating unique ids" ? – Svetlin Zarev Dec 15 '13 at 17:53
  • If you want to generate unique IDs, as your title suggests, look at UUIDs. – james.garriss Dec 15 '13 at 21:11

2 Answers2

2

Use regular expressions. Here is a regexp that does what you need - Regular expression to extract text between square brackets.

Community
  • 1
  • 1
facetoe
  • 719
  • 1
  • 6
  • 20
0

Solution in pseudo-code.

  1. Replace all occurrences of [ with ''.
  2. Replace all occurrences of ] with ''.
  3. Split that string on ' ' (the space character).
  4. Print the first element of the resulting array.
  5. Print a comma.
  6. Print the last element in the array.
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433