-1

I have a program where I have a text file, I have to read that text file and display output in term of returning product id to return product object with properties id,name,qty,price. But i have to do it using string tokenizer. If anybody has any idea regarding this please share.. text file contains following data:

id-name-qty-price
101-TV-80-9999
102-laptop-70-898989
103-tablet-50-8888

This is what I have attempted up till now I am able to display data but I am facing problem displaying output:

public static void main(String[] args) throws FileNotFoundException {
    // TODO code application logic here
    FileReader r = new FileReader("product.txt");
    String [] lines = new String[100];
    try{
        BufferedReader br =  new BufferedReader(r);
        int x = 0;
        String s;
        while((s = br.readLine()) != null){
            lines[x] = s;
            x++;
        }
    }
    catch(IOException e) {
        System.exit(0);
    }
    for(String st: lines)
        System.out.println(st);
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
John
  • 73
  • 1
  • 8
  • 1
    Show us what you've attempted! – Adam Jul 02 '15 at 13:52
  • In the future, edit your question and put the code there :D – Adam Jul 02 '15 at 14:13
  • I am so sorry for this mistake, Actually I don't have any idea about this I am trying to learn and it new concept for me. Thanks for reminder – John Jul 02 '15 at 14:17
  • Just helping you help us get you an answer. If anyone ever asks you to show code. You'll see the edit button below your question. Click that, then paste your code below your question so that we can see it. Much quicker than having it in a comment or answer. – Adam Jul 02 '15 at 14:19
  • Yup got it Adam Thank you so much. – John Jul 02 '15 at 14:23

1 Answers1

0

Read the file line by line and do something like this on each line

    StringTokenizer st = new StringTokenizer(line, "-", false);
    while(st.hasMoreTokens()) {
        System.out.println(st.nextToken());
    }
DrB
  • 264
  • 1
  • 3
  • 14