Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Import CSV data into 2d array then compare each row

  1. #1
    Junior Member
    Join Date
    May 2019
    Location
    Lebanon
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation Import CSV data into 2d array then compare each row

    Hey Guys;

    I have a CSV file that i need to compare each row with the rest to see if they are equal or not.
    I knew how to create an array list but i dont know how to carry on to compare each row alone with the others to see if their is other equal to it-Knowing that the number of rows and colomns in the CSV file is unknown- and save the result in a Right/Wrong variable.

    this is my code to create the arraylist:
    List<List<String>> records=new ArrayList<>();
     
    		try(BufferedReader br=new BufferedReader(new FileReader("D:\\traincpu.csv"))) {
    			String line=br.readLine();      //To skip first line added readline()
    			int count=0;                    //   to count row numbers
     
     
    		  	while((line=br.readLine()) !=null) {
    				String[] values =line.split(";");
    				 count=count+1;     
    				records.add(Arrays.asList(values));
     
    			}
    		  	int rowcont=0;
    		 // 	while((line=br.readLine())!="\n") {
    		  		rowcont++;
    		  //	}

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Import CSV data into 2d array then compare each row

    Why not read each row as a String and compare the strings? Otherwise, what happens if you have the following four values? And why are you splitting on a semi-colon and not a comma?

    1, 2, "three, four", five

    Regards,
    Jim

  3. The Following User Says Thank You to jim829 For This Useful Post:

    alikd (May 9th, 2019)

  4. #3
    Junior Member
    Join Date
    May 2019
    Location
    Lebanon
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Import CSV data into 2d array then compare each row

    Hello Jim, Thank you for your fast reply.

    Each row in the file represent an attribute for a point; that point is a data i need to give it a weight then calculate the euclidean distance between this CSV file and another file which contain test data.(Yes its Machine learning algorithm)

    So,the String option i think is not good idea and the output of the values above: 1, 2, "three, four", five is

    Exception in thread "main" java.io.IOException: number expected, read Token[three, four], line 18
    Plz i need help since im not a programmer but i need to edit a code in weka application for my collage studies.

  5. #4
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Import CSV data into 2d array then compare each row

    What exactly do the rows look like? Can you provide an example?

    Regards,
    Jim

  6. #5
    Junior Member
    Join Date
    May 2019
    Location
    Lebanon
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Import CSV data into 2d array then compare each row

    MYCT,MMIN,MMAX,CACH,CHMIN,CHMAX,class
    240,512,2000,8,1,5,11
    105,2000,4000,8,3,8,22

    it may contain also string data not only numeric.

    This is an example of 2 rows of the file
    note that i already collected the data from another file and saved them as csv file as the original file contain text.

  7. #6
    Junior Member
    Join Date
    May 2019
    Location
    Lebanon
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Import CSV data into 2d array then compare each row

    Thank you that is so helpful; but i still have a problem in importing unknown number of rows from a CSV file.


    plz check the following:

    public class array {
     
    	public static void main(String[] args) {   
     
    		//  String[] records==new ArrayList<>();   
     
    		String fileName= "example.csv";
            File file= new File(fileName);
     
     
            try{
                Scanner inputStream= new Scanner(file);
                while(inputStream.hasNext()){
     
                String data= inputStream.next();
                String[] records = data.split(",");
                System.out.print(records[2]+ ",");
                }
                inputStream.close();
             } catch (Exception e) {                                                       
    			 System.out.println(e.getMessage());                                      
    			 System.exit(-1);   
        }
     
     
    		  for (int i = 0; i < records.length - 1; i++) {                              // error: records cannot be resolved to a variable
    			 int[] r1 = toIntArray(records[i]);                                     // error: records cannot be resolved to a variable     
    			 inner:                                                                   
    			 for (int k = i + 1; k < records.length; k++) {                     // error: records cannot be resolved to a variable                            
    				int[] r2 = toIntArray(records[k]);                             // error: records cannot be resolved to a variable          
    				if (r1.length == r2.length) {                                         
    				   for (int r = 0; r < r1.length; r++) {                              
    					  if (r1[r] != r2[r]) {                                           
    						 continue inner;                                              
    					  }                                                               
    				   }                                                                  
    				   System.out.println("row " + i + " equals to row " + k);            
    				}                                                                     
    			 }                                                                        
    		  }                                                                           
    		}   
     
     
    		public static int[] toIntArray(String s) {                                     
    		  String[] tokens = s.split(",");                                             
    		  int[] temp = new int[tokens.length];                                        
    		  try {                                                                       
    			 for (int i = 0; i < tokens.length; i++) {                                
    				temp[i] = Integer.parseInt(tokens[i]);                                
    			 }                                                                        
    		  }                                                                           
    		  catch (Exception e) {                                                       
    			 System.out.println(e.getMessage());                                      
    			 System.exit(-1);                                                         
    		  }                                                                           
    		  return temp;                                                                
    		}
     
    }


    I dont know how to call rows from the csv file after i saved the data into an array.


    Regards

Similar Threads

  1. Replies: 0
    Last Post: October 4th, 2017, 12:24 PM
  2. Replies: 2
    Last Post: December 13th, 2013, 12:01 AM
  3. Problem with executing data from CSV file
    By 28mikes in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 15th, 2013, 05:48 PM
  4. Importing data from .csv into array
    By mikeg in forum Collections and Generics
    Replies: 3
    Last Post: November 12th, 2010, 05:59 AM
  5. Cannot update data in .txt/.csv file
    By Azriq007 in forum JDBC & Databases
    Replies: 2
    Last Post: October 16th, 2010, 09:16 PM

Tags for this Thread