Results 1 to 6 of 6
  1. #1
    pokadean is offline Member
    Join Date
    Jul 2013
    Posts
    12
    Rep Power
    0

    Default 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);
    }
    so basically now I have to ArrayList's where I have stored the two types of codes...

    BUT when I'm trying to do something like

    Java Code:
        public ArrayList<String> getProductCodesSupplier(String supplierCode){
        	return null;
        	}
    Meaning that, given my code i want to retrieve ALL the products sent by him, HOW can I actually "reference" in some way the two arraylists?
    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. :)

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,341
    Rep Power
    5

    Default 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,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    pokadean is offline Member
    Join Date
    Jul 2013
    Posts
    12
    Rep Power
    0

    Default 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
    }
    but now when i'm actually trying to retrieve the list of products of a certain supplier it doesn't give me back the actual list :/
    I'm close to the solution but not yet..

  4. #4
    pokadean is offline Member
    Join Date
    Jul 2013
    Posts
    12
    Rep Power
    0

    Default 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);
    				}
    because it doesn't work

  5. #5
    pokadean is offline Member
    Join Date
    Jul 2013
    Posts
    12
    Rep Power
    0

    Default Re: Reading from a File, ArrayList and "reference"

    i did it using elements.lenght!

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,341
    Rep Power
    5

    Default 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(",");
    It should allocated just enough space to store the values. So you can use the length field of the arrays.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  2. Replies: 3
    Last Post: 10-30-2012, 03:06 PM
  3. Replies: 1
    Last Post: 10-03-2011, 03:42 AM
  4. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  5. Replies: 1
    Last Post: 07-16-2007, 06:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •