0

If I have a bufferedReader which reads lines from the following text file. How can I make the reader read for example the tag ITEM_TYPE of the first ITEM and then return and reads the tag CODE and continue to all the other tags( of the first ITEM)?

 ITEM_LIST
{
  ITEM
{
    CODE ADLS443
    ITEM_TYPE tv
    MODEL IDL32KJX4300
    MODEL_YEAR 2016
    MANUFACTURER Samsung
    PRICE 350
    PANEL_TYPE LCD
    DIMENSIONS 32
    RESOLUTION "Full HD"
    INTERFACES "HDMI USB SCART"
    PIECES 43
}
ITEM
{
    CODE KD444211
    ITEM_TYPE tv
    MODEL KDL49DDR4600
    MODEL_YEAR 2014
    MANUFACTURER Sony
    PRICE 600
    PANEL_TYPE LED
    DIMENSIONS 49
    RESOLUTION "Ultra HD"
    INTERFACES "HDMI USB SCART Ethernet WiFi"
    PIECES 8
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • The reader will read the file from top to bottom. What are you trying to do? I suspect the real issues is how to handle the results as you parse them. – cyroxis May 12 '16 at 15:54
  • I am trying to create objects depending on the tag ITEM_TYPE (e.g. if ITEM_TYPE is tv i create a tv object which will have as fields the rest of the ITEM_TAG). –  May 12 '16 at 15:59
  • BTW, do you have control ofter the file format? If you can switch your format to something such as JSON the parsing will be easier (libraries such as Gson, or Jackson will do it for you). – cyroxis May 12 '16 at 16:13
  • The file format for this project should be spesific and I can't change it . –  May 12 '16 at 16:18

1 Answers1

1

You can add an intermediate step to your parsing.

  1. Parse all key value pairs (for section) into a map.
  2. Use map values to create desired object.

Not knowing the rest of your code here is some pseudocode of how you could parse your data.

Map<String, String> values = new HashMap<>();
// For each key value pair in section
  String key = // parse key name
  String value = // parse key value
  values.put(key, value);

String type = values.get("ITEM_TYPE");
if (type.equals("tv"))
  TvObject tv = new TvObject(values);
}

Edit:

You should not be parsing for specific key values, but for the format of your file.

ITEM
{
    <key> <value>
    <key> <value>
    <key> <value>
}

Here is an example of how to parse an INI file that you could expand upon.

Community
  • 1
  • 1
cyroxis
  • 3,661
  • 22
  • 37
  • I was going to suggest [loading the whole file into a list,](http://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#readAllLines-java.nio.file.Path-java.nio.charset.Charset-) but this is a better approach which makes it easier to parse the file stream-wise, and not worry so much about the total size of the file. – erickson May 12 '16 at 16:12
  • Thanks, but how can I read the key and value from the file and put them in the map if the tags order is not specific ? –  May 12 '16 at 16:16
  • What have you tried? Have you ready any tutorials on parsing? I am glad to help but am not going to write your project for you. – cyroxis May 12 '16 at 16:19
  • I used an if statement and I checked for each line: if it starts with CODE then I put the code and the rest of the line in the map else if it starts with ITEM_TYPE I put the ITEM_TYPE and the rest of the line in the map etc. –  May 12 '16 at 16:37
  • You should be looking for the format of the file generically because that is the grammar of your file. I updated answer with a link to an example. – cyroxis May 12 '16 at 17:04