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.

black sensei
  • 6,528
  • 22
  • 109
  • 188
Manimalis
  • 173
  • 1
  • 1
  • 11
  • refer to http://stackoverflow.com/a/3487156/3393095 – Rodrigo López May 13 '15 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. – MadConan May 13 '15 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(). – Andy Thomas May 13 '15 at 15:39
  • Excel will accept any character as a delimiter. Tab and comma are the typical ones used. – MadConan May 13 '15 at 15:49

2 Answers2

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.

MadConan
  • 3,749
  • 1
  • 16
  • 27
  • 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 May 18 '15 at 07:06
  • @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? – MadConan May 18 '15 at 11:54
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.

Kapcash
  • 6,377
  • 2
  • 18
  • 40