Solved

How to move the specific column in CSV to first column using java

Posted on 2009-05-12
7
277 Views
Last Modified: 2013-11-13
Hi I want to move the specific column with values to the first column in CSV. will it be possible using java code
ex:
COLUMN1 COLUMN2 COLUMN3
a1             b1              c1
a2             b2              c2
a2             b3              c3

and i want to be
if(col[i].equals("COLUMN3"))

COLUMN3 COLUMN2 COLUMN3
c1             b1             a1
c2             b2             a2
c3            b3              a3


I am using following code to read csv

BufferedReader csvReader;

            csvReader = new BufferedReader(new

                    FileReader(csvFileName));

            int fieldCount = 0;

            String[] csvFields = null;

            StringTokenizer stringTokenizer = null;
 

String curLine = csvReader.readLine();

            if(curLine != null)

            {

                stringTokenizer = new StringTokenizer(curLine, ",");

                fieldCount = stringTokenizer.countTokens();

                if(fieldCount > 0)

                {

                    csvFields = new String[fieldCount];

                    int i=0;

                    while(stringTokenizer.hasMoreElements()){

                        csvFields[i++] =

                                String.valueOf(stringTokenizer.nextElement());

             

                    }

                }

            }

            

            for(int i=0;i<csvFields.length;i++){

            	String csvField=csvFields[i].toUpperCase();

            	csvFields[i]=csvField.replaceAll(" ","_");

            	if(csvFields[i].equals("ETHNIC_ORIGIN")){

            		

            	}

            	

            }
 

            while((curLine = csvReader.readLine()) != null)

            {

            	String strArr[] = curLine.split(",");

            	String result ="";

            	 String temp ="";

            	for ( int i =0 ;i<strArr.length;i++)

            	{

            	  if( strArr[i].equals(""))

            	    temp = "(Blanks),";

            	 else

            	    temp = strArr[i] + ",";

            	result = result +  temp;

            	}

            	

            	curLine=result;

            	

            	int pp = 0;

            	  while (true) {

            	    int found = curLine.indexOf("\"", pp);

            	    if (found == -1) break;

            	    int start = found + 1; // start of actual quote

            	    int end = curLine.indexOf("\"", start);

            	    

            	    String str1=curLine.substring(start, end);

            	    str1=str1.replaceAll(",","");

            	    String str2="";

            	    str2=curLine.substring(end+1, curLine.length());

            	    

            	    if(found!=0){

            	    	curLine=curLine.substring(0, start-1)+str1+str2;

            	    	

            	    }

            	    else{

            	     	curLine=str1+str2;

            	        

            	    }

            	    pp = end + 1;  // advance i to start the next iteration

            	  }

				

                stringTokenizer = new StringTokenizer(curLine, ",");

------

Open in new window

0
Comment
Question by:ramadevimandala
  • 3
  • 2
  • 2
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24363381
I would use a proper CSV api such as Ostermiller but all you need to do is to swap the columns in the array
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24363389

String temp = array[2];

array[2] = array[0];

array[0] = temp;

Open in new window

0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 24370281
simplest would be to use a loop to switch the columns once it is read
another option would be too use a Map for your columns making them easily accessible
depends on your requirements

let me know if you need help with it

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:ramadevimandala
ID: 24372010
Hi,
Thank you for ur help , I am trying to loop thru columns and swapping the values, let me try once this
0
 
LVL 92

Expert Comment

by:objects
ID: 24372033
no worries, should be around for a few hours of you get stuck

0
 

Author Comment

by:ramadevimandala
ID: 24429408
swaping the elements in loop is working fine.. thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24429433
ramadevimand, that's good. Can you post the code you're now using?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
recursion example 16 111
Windows and other UI names - The Proper names for the Various elements! 1 55
simplest php form 3 57
varialbe initialization 11 29
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This is about my first experience with programming Arduino.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now