?
Solved

column format

Posted on 2006-04-03
11
Medium Priority
?
186 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:ellandrd
  • 6
  • 4
11 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 1800 total points
ID: 16368077
// 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
 
LVL 16

Author Comment

by:ellandrd
ID: 16368151
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
 
LVL 92

Expert Comment

by:objects
ID: 16368164
if the file doesn't have a set format then its a much trickier job.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 200 total points
ID: 16368173
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
 
LVL 16

Author Comment

by:ellandrd
ID: 16368199
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
 
LVL 16

Author Comment

by:ellandrd
ID: 16368200
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
 
LVL 16

Author Comment

by:ellandrd
ID: 16368225
i can display both header[0] and [1] but then i get out of bounds for the next split:

String[] s = str.split(",");
0
 
LVL 92

Accepted Solution

by:
objects earned 1800 total points
ID: 16368239
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
 
LVL 16

Author Comment

by:ellandrd
ID: 16368374
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
 
LVL 92

Assisted Solution

by:objects
objects earned 1800 total points
ID: 16368399
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
 
LVL 16

Author Comment

by:ellandrd
ID: 16368435
Im having a problem writing the 2 strings; age and phone to my file but i'll open another question for this...
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month14 days, 14 hours left to enroll

840 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