0

I'm a beginner in Java and a geomatics student.

I am using IntelliJ to export data from an XTF image that contains various information such as coordinates, timestamp etc...

This is my code:

FileWriter writer = new FileWriter( "DonneesPings1.csv" );
FileWriter writer2 = new FileWriter( "DonneesPings2.csv" );
writer.write( "X,Y\n" );
int limit = 0;
for (XtfPing ping : xtf.getPings())
{
    if ( limit < 10000 )
    {
        writer.write( Double.toString( ping.x ) );
        writer.write( "," );
        writer.write( Double.toString( ping.y ) );
        writer.write( "\n" );
    }
    else {
        writer2.write( Double.toString( ping.x ) );
        writer2.write( "," );
        writer2.write( Double.toString( ping.y ) );
        writer2.write( "\n" );
    }
    limit++;
}
writer.close();

The snippet above creates two .csv files ("DonneesPings1.csv" and "DonneesPings2") but its format is not good. Data are only put in one column with a comma as the separator.

Example:

Column A
    X,Y
    0.000000000,-00.00000000
    0.022222222,-02.00000000

I would like the x coordinates to be in one column and the y coordinates in another.

Example:

   Column A          Column B
      X                 Y
0.000000000       -00.00000000
0.022222222       -02.00000000

I can make it in Excel, but I would like to do it programmatically with IntelliJ.

4
  • refer to stackoverflow.com/a/3487156/3393095 Commented May 13, 2015 at 15:04
  • 1
    maybe I'm missing something, but what you have looks like what you want. The values in the "not good" example are in two columns. Commented May 13, 2015 at 15:11
  • Alternatives to CSV include: 1) tab-separated values, which would allow you to import into Excel, and 2) formatted text fields, which you can do with String.format(). Commented May 13, 2015 at 15:39
  • Excel will accept any character as a delimiter. Tab and comma are the typical ones used. Commented May 13, 2015 at 15:49

2 Answers 2

2

CSV stands for Comma Separated Values. The commas could be thought of as column separators. Your "bad example" is a two column file and your code is writing the values correctly.

X,Y  // <- 2 columns
0.000000000,-00.00000000 // <- 2 columns
0.022222222,-02.00000000 // <- 2 columns

If you want something that is visually laid out in plain text like your "good" example, you can use String.format or something similar, but the resulting file wouldn't really be a CSV.

Sign up to request clarification or add additional context in comments.

2 Comments

you're right, so it' not visually cut into two colums but if I want to reuse this data, my IDE will understand that It works as if there were two columns thanks to the comma separator, right ?
@Manimalis I'm not sure why you want to feed the CSV to the IDE. IDE's are for developing applications and don't normally consume application output. What do you intend to do with the CSV you generate?
0

You can use String.format("%-30s, Double.toString(ping.x) ) to write a line on 30 characters. Empty ones will be filled with blank space. Sorry, I don't know any trick to center your string.

Check out Formatter class to know more about formatting a String.


But, as MadConan said, your 'not good example' is a well formatted CSV. What you want will not be a correct CSV because it will not have any character to separate your data.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.