Results 1 to 2 of 2

Thread: Help with code.

  1. #1
    Vahevahe is offline Member
    Join Date
    Aug 2015
    Posts
    3
    Rep Power
    0

    Default Help with code.

    Hey guys i need help with my code. The code reads 2 collums of data (item number and Quantity of item sold) and places then in a arraylist. then checks if there is any duplicate item numbers and adds there quantities together. I am having trouble with adding the Quantitys together and getting rid of the duplicate row so it dosnt loop again and cause problems. Any help please!

    Java Code:
    import java.awt.List;
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    
    public class ReadCVS {
    
    	public static void main(String[] args) {
    		ReadCVS obj = new ReadCVS();
    		obj.run();
    		
    	 }
    	 
    	  public void run() {
    	 
    		String csvFile = "/users/asmikiskanyan/desktop/Sales.csv";
    		BufferedReader br = null;
    		String line = "";
    		String cvsSplitBy = ",";
    	
    	 
    		try {
    	 
    			br = new BufferedReader(new FileReader(csvFile));
    			br.readLine(); 
    			
    			int[][] mylist = new int [2][50]; 
    			int[][] sum = new int [2][50]; 
    			 
    			while ((line = br.readLine()) != null) {
    				int in = 0;
    				String[] country = line.split(cvsSplitBy); 
    				//System.out.println(country[1] + " " + country[2]);
    				
    				int a = Integer.parseInt(country[1]);
    				int b = Integer.parseInt(country[2]); 
    				
    				mylist [in][0] = a; 
    				mylist [in][1] = b; 
    				
    				for (int i = 0; i < mylist.length ; i++){
    					  if (mylist [in][0]==mylist[i][0]){
    				
    					mylist [in][1] =+ mylist [i][1]; 
    					
    					
    			
    					 //NEED HELP HERE****
    					   					 
    							  }
    					  
    					  
    				}
    				
    				//System.out.println(mylist [in][0] + " " +mylist [in][1]);
    				in++; 
    			}
    			
    		
    			
    		
    			
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		} finally {
    			if (br != null) {
    				try {
    					br.close();
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    			}
    		}
    	 
    		
    	  }
    	  
    	}
    SAMPLE OUTPUT
    10002 4
    10004 6
    10008 2
    10010 3
    10010 3
    10007 10
    20003 4
    20003 7
    30019 1
    30020 9
    10004 9
    10006 7
    20005 5
    30004 10
    20004 2
    30002 7
    30013 6
    20005 5
    30006 1
    30006 7
    30007 2
    30012 10
    30015 7
    30015 3
    10001 5
    20001 8
    30007 3
    30014 3
    30015 6
    30016 2
    10001 8
    10003 8
    10007 5
    10007 8
    20005 6
    20006 10
    30003 4
    30011 7
    30014 7
    30016 5
    20003 1
    30003 4
    30010 5
    30011 8
    30012 1
    30014 9
    30016 4
    30018 5
    10006 1
    10010 9
    20005 5
    30001 1
    30004 9
    30007 5
    30009 1
    30014 6
    30016 5
    30016 2
    30020 8
    10004 3
    10004 7
    10008 2
    20001 4
    20003 7
    20004 6
    20006 2
    30001 1
    30003 7
    30004 1
    30004 10
    30005 2
    30006 7
    30017 10
    30018 2
    30004 6
    10009 9
    10009 7
    10010 10
    20005 3
    30002 1
    30004 2
    30004 8
    30013 3
    30019 3
    10004 10
    10007 5
    30001 7
    30003 6
    30006 2
    30006 3
    30007 2
    30013 8
    30016 8
    10003 6
    10004 9
    10004 2
    10006 8
    10010 1
    20003 9
    20003 10
    30001 8
    30002 1
    30013 5
    30015 7
    30018 1
    30019 10
    30020 9
    10008 1
    10009 3
    20004 10
    30010 7
    30012 9
    30014 7
    30017 4
    20004 5
    30005 8
    30010 7
    30011 1
    10004 9
    10004 4
    10006 5
    10010 7
    20001 3
    20003 2
    20004 7
    30004 8
    30010 2
    30012 5
    30019 8
    30020 1
    10008 7
    30001 9
    30003 10
    30003 8
    30006 5
    30008 10
    30009 7
    30009 8
    30015 9
    30017 9
    30018 7
    30020 1
    10001 7
    10002 10
    10003 3
    10005 4
    10010 4
    20001 4
    20002 1
    20005 5
    30006 9
    30006 6
    10003 7
    10003 4
    10004 6
    10009 6
    20002 6
    20004 3
    20005 3
    30001 9
    30005 2
    30006 10
    30008 6
    30009 6
    30011 10
    30013 8
    30013 3
    30013 5
    30015 1
    30019 10
    10002 6
    10004 7
    10006 3
    10006 2
    20002 6
    20004 7
    20004 7
    30005 4
    30006 8
    30007 3
    30008 6
    30009 8
    30014 5
    30015 10
    30015 9
    30018 3
    30019 2
    30019 9
    30020 1
    10006 10
    Last edited by Vahevahe; 09-04-2015 at 12:32 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Help with code.

    I recommend you use a Map implementation. Maps are made for this type of application. If you can't use those, then I recommend you read the values in first. Then use some nested loops to extract the duplicates. Also, the short cut for addition is += not =+.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Posting code to forum : Code to code tags
    By rushtojess in forum Suggestions & Feedback
    Replies: 2
    Last Post: 04-21-2015, 08:16 PM
  2. Replies: 1
    Last Post: 03-25-2012, 03:40 AM
  3. Replies: 0
    Last Post: 08-07-2011, 08:32 PM
  4. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

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