java - How to parse a String into multiple arrays -
good day!
i making mini bookstore program , required read file based customer buys on specified counter follows:
counter 4,book1 2,book2 2,book3 2,tender 100.00 counter 1,book1 2,book2 1,book3 3, book4 5,tender 200.00 counter 1,book3 1,tender 50.00
in short format is: counter -> items bought -> tender
i tried doing not efficient:
public list<string> getorder(int i) { list <string> tempqty = new arraylist<string>(); string[] orders = orderlist.get(0).split(","); (string order : orders) { string[] fields = order.split(" "); tempqty.add(fields[i]); } return tempqty; }
how can read file , @ same time, ensures put on correct array? besides counter , tender, need know name of book , qty price , computer total price. need multiple arrays store each values? suggestions/ codes highly appreciated.
thank you.
map<string, integer> itemsbought = new hashmap<string, integer>(); final string counter = "counter"; final string tender = "tender"; string[] splitted = s.split(","); (string str : splitted) { str = str.trim(); if (str.startswith(counter)) { //do want counter } else if (str.startswith(tender)) { //do want tender } else { //process items, e.g: string[] iteminfo = str.split(" "); itemsbought.put(iteminfo[0], integer.valueof(iteminfo[1])); } }
Comments
Post a Comment