I am trying to write a program that maps a product to its description. I have attached the text in the end. This is the code I have come up with. It compiles fine but does not give me the result properly..

import java.io.*;
import java.util.*;
 
public class InstanceItem{
    HashMap itemToDescription;
    BufferedReader bin;
    String filename = "sampledoc.txt";
    String line = "";
    String line2 = "";
    String line1 = "";
    
    public void main(String[] args){
        try{
         itemToDescription = new HashMap();
         bin = new BufferedReader(new FileReader(filename));
         while((line = bin.readLine())!=null){
             StringTokenizer s = new StringTokenizer(line);
             String name = s.nextToken();
             
             if(s.nextToken().equalsIgnoreCase("of")){
                 line2 += line;
                 line = bin.readLine();
                 s = new StringTokenizer(line);
                 s.nextToken();
                 while(!(s.nextToken().equalsIgnoreCase("of"))){
                     line2 +=line;
                 }
                 itemToDescription.put(name, line2);
                 
             }
             
         }
                 }catch(IOException e) {
            System.err.println(e.getMessage());
        }
    }
}

Please take a look at my code: My input file looks like this:

([description_001] of Book
text text text text text
text text text text text
text text text text text)

([description_002] of Pen
text text text text text
text text text text text
text text text text text)

([description_003] of Magazine
text text text text text
text text text text text
text text text text text)

([description_004] of Book
text text text text text
text text text text text
text text text text text)

([description_005] of Magazine
text text text text text
text text text text text
text text text text text)


Is there a better way. What modification should I do to my code.

Thanks,
jeni

As a quick but useful suggestion have you considered using a whole class to represent just one of your data items. It often works well because you can then seperate the handling of multiple items from the detail of each item and it makes it much easier then to add a field to each item etc. etc.

Just a thought.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.