Results 1 to 6 of 6
- 09-12-2013, 08:23 PM #1
Member
- Join Date
- Jul 2013
- Posts
- 12
- Rep Power
- 0
Reading from a File, ArrayList and "reference"
Hi everyone! So this is my piece of program that basically what it does is reading rows from a file.
Every row has several fields separated by "," and can be of different type (rows starting with P, C, ecc..)
One of the types of rows has a field specifying a code of who sends a product and another field about the code of the actual product delivered.
So on my file I will have more rows with the same code of the person who sends products.
What I'm trying to ask you is: how can i store successfully those codes of suppliers (they can be repeated, i mean the same supplier code can appear in more rows so TreeMap is not an option) referenced each time to the actual product code delivered?
On my program I did something like:
Java Code:ArrayList<String> fprcode = new ArrayList<String>(); ArrayList<String> su= new ArrayList<String>(); //... //... //... part where i read from file using SPLIT until I reach the part where i'm checking for the F type rows if (elements[0].equals("F")){ String character=elements[0]; String supplCode=elements[1]; String productCode=elements[2]; fprcode.add(supplCode); su.add(productCode); }
BUT when I'm trying to do something like
Java Code:public ArrayList<String> getProductCodesSupplier(String supplierCode){ return null; }
Is arraylist a bad idea? where should I store my two elements so that they are always together and referenced one another given that I need to have duplicates allowed?
Thanks guys, hope I made myself clear. :)
- 09-12-2013, 08:28 PM #2
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Reading from a File, ArrayList and "reference"
Well, I am not quite certain I understand the problem. It hints at using a Map of Lists. Where the key is the supplier and the List it maps to are the codes. Or perhaps a Map of Maps would be better.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 09-12-2013, 09:03 PM #3
Member
- Join Date
- Jul 2013
- Posts
- 12
- Rep Power
- 0
Re: Reading from a File, ArrayList and "reference"
Ok, so if I do something like
Java Code:public void getResult(Map<String, ArrayList<String>> result) { for (String s: suu){ result.put(s, su); } //where ArrayList<String> suu= new ArrayList<String>(); //list of supplier codes ArrayList<String> su= new ArrayList<String>(); //list of product codes }
I'm close to the solution but not yet..
- 09-12-2013, 09:31 PM #4
Member
- Join Date
- Jul 2013
- Posts
- 12
- Rep Power
- 0
Re: Reading from a File, ArrayList and "reference"
Just a thing: how can I mark the end of row if some of the rows have a variable length?
like, one row has 3 fields and another 4?
I know that i CAN'T do something like:
Java Code:String character=elements[0]; String supplCode=elements[1]; String productCode=elements[2]; suu.add(productCode); if(elements[3] != null){ String productCode2=elements[3]; suu.add(productCode2); } if(elements[4] != null){ String productCode4=elements[4]; suu.add(productCode4); }
- 09-12-2013, 09:37 PM #5
Member
- Join Date
- Jul 2013
- Posts
- 12
- Rep Power
- 0
Re: Reading from a File, ArrayList and "reference"
i did it using elements.lenght!
- 09-12-2013, 10:03 PM #6
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Reading from a File, ArrayList and "reference"
You said the values are separated by commas. So if you do the following:
Java Code:String line; String [] vals = line.split(",");
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads
-
access denied("java.net.SocketPermission" "127.0.0.1:1099" "connect,resolve")
By klspepper in forum New To JavaReplies: 0Last Post: 12-07-2012, 09:29 AM -
Convert string operation symbols "+", "-", "/", "*" etc.
By Googol in forum New To JavaReplies: 3Last Post: 10-30-2012, 04:06 PM -
What is "reference"? Am I understanding it correct by smiplifying it this way?
By Callofdudey in forum New To JavaReplies: 1Last Post: 10-03-2011, 04:42 AM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 08:35 AM -
ArrayList: Exception in thread "main" java.lang.NullPointerException
By susan in forum New To JavaReplies: 1Last Post: 07-16-2007, 07:32 AM
Bookmarks