column format

A file is formated like this:

"age","phone number"
"Ellandrd","Leeds","Sept",23,0123456789
"Mark","Aberdeen","Feb",34,0123456789
"Roddy","Glasgow","Mar","Student",24,0123456789

Number of columns on the first row could be from 0 to any numbers, n.

How can I convert it to the following format:

"Ellandrd","Leeds","Sep","age",23
"Ellandrd","Leeds","Sep","phone number",0123456789
"Mark","Aberdeen","Feb","age",34
"Mark","Aberdeen","Feb","phone number",0123456789
"Roddy","Glasgow","Mar","age",24
"Roddy","Glasgow","Mar","phone number",0123456789
LVL 16
ellandrdAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
// split first line to get headers
String[] headers = line.split(",");


Then for each subsequent line

// first split line using comma as seperator
String[] s = line.split(",");
// put toegether two new lines
String age = s[0]+","+s[1]+","+s[2]+","+headers[0]+","+s[3];
String phone = s[0]+","+s[1]+","+s[2]+","+headers[1]+","+s[4];
0
ellandrdAuthor Commented:
why split the headers?  My file could be like this either:

"age","phone number","Ellandrd","Leeds","Sept",23,0123456789
"Mark","Aberdeen","Feb",34,0123456789,"Roddy","Glasgow","Mar"
"Student",24,0123456789

and i'll want ot get same output...
0
objectsCommented:
if the file doesn't have a set format then its a much trickier job.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Mayank SAssociate Director - Product EngineeringCommented:
Once you've got rid of the 1st two tokens ("age" and "phone number"), you will need to read (ignoring new-lines) the next 5 tokens and then display them in the required format. Then read the next 5 (ignoring new-lines) and display them, etc
0
ellandrdAuthor Commented:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
        at org.seandelaney.ColumnFormatting.process(ColumnFormatting.java:41)


public class ColumnFormatting {
    public ColumnFormatting() {
        try {
            BufferedReader in = new BufferedReader( new FileReader( "etc\\columnformatting.txt" ) ) ;
            String str ;
            while ( ( str = in.readLine() ) != null ) {
                process( str ) ;
            }
            in.close() ;
        } catch ( IOException e ) {
        }
    }
   
    private void process( String str ) {
        String[] headers = str.split(",");
        String[] s = str.split(",");
        String age = s[0]+","+s[1]+","+s[2]+","+headers[0]+","+s[3];
        String phone = s[0]+","+s[1]+","+s[2]+","+headers[1]+","+s[4];
       
        System.out.println( s ) ;
    }

    public static void main( String[] args ) {
        new ColumnFormatting() ;
    }
}
0
ellandrdAuthor Commented:
Also just let say my file format is this:

"age","phone number"
"Ellandrd","Leeds","Sept",23,0123456789
"Mark","Aberdeen","Feb",34,0123456789
"Roddy","Glasgow","Mar","Student",24,0123456789
0
ellandrdAuthor Commented:
i can display both header[0] and [1] but then i get out of bounds for the next split:

String[] s = str.split(",");
0
objectsCommented:
public class ColumnFormatting {
    public ColumnFormatting() {
        try {
            BufferedReader in = new BufferedReader( new FileReader( "etc\\columnformatting.txt" ) ) ;
            String str = in.readLine();;
            String[] headers = str.split(",");
            while ( ( str = in.readLine() ) != null ) {
                process( headers, str ) ;
            }
            in.close() ;
        } catch ( IOException e ) {
        }
    }
   
    private void process( String[] headers, String str ) {
        String[] s = str.split(",");
        String age = s[0]+","+s[1]+","+s[2]+","+headers[0]+","+s[3];
        String phone = s[0]+","+s[1]+","+s[2]+","+headers[1]+","+s[4];
       
        System.out.println( s ) ;
    }

    public static void main( String[] args ) {
        new ColumnFormatting() ;
    }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ellandrdAuthor Commented:
is there a way of missing out the "Student" string in the file?

as it prints out:

"Ellandrd","Leeds","Sep","age",23
"Ellandrd","Leeds","Sep","phone number",0123456789
"Mark","Aberdeen","Feb","age",34
"Mark","Aberdeen","Feb","phone number",0123456789
"Roddy","Glasgow","Mar","age","Student"
"Roddy","Glasgow","Mar","phone number",24

and im really looking for this:

"Ellandrd","Leeds","Sep","age",23
"Ellandrd","Leeds","Sep","phone number",0123456789
"Mark","Aberdeen","Feb","age",34
"Mark","Aberdeen","Feb","phone number",0123456789
"Roddy","Glasgow","Mar","age",24
"Roddy","Glasgow","Mar","phone number",0123456789
0
objectsCommented:
change it to:

       String age = s[0]+","+s[1]+","+s[2]+","+headers[0]+","+s[s.length-1];
        String phone = s[0]+","+s[1]+","+s[2]+","+headers[1]+","+s[s.length-1];
0
ellandrdAuthor Commented:
Im having a problem writing the 2 strings; age and phone to my file but i'll open another question for this...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.