-1

My code uses BufferedReader to read columns of data in a text file. The text file looks like:

Year.....H2OIN....CO2IN
0.000......0.0..........0.0
1.000......2.0..........6.0
2.000......3.0..........7.0
3.000......4.0..........8.0

My formatting code looks like:

try {
  FileInputStream file = new FileInputStream(inputFile);
  BufferedReader in = new BufferedReader(new InputStreamReader(file));

  f = new Formatter("M:\\TESTPACK\\AL6000803OUT.TXT"); 

  while((line = in.readLine()) != null) {
    if (line.startsWith("    0.000"))
      break;
  }

  while((line = in.readLine()) != null) {
    stream = line.split(parse);
    start = line.substring(6,9);

    if (start.equals("000")) {
      H2OIN = Double.parseDouble(stream[1]);
      CO2IN = Double.parseDouble(stream[2]);

      f.format("%s ", H2OIN);
      f.format("%s ", CO2IN);
    }
  } 
}catch (FileNotFoundException e) {
}catch (IOException e) {
}

f.close();  

However, my output file looks like:

2.0 6.0 3.0 7.0 4.0 8.0

While I want it to look like:

2.0 3.0 4.0
6.0 7.0 8.0

I need a suggestion for how to apply formatting to the data strings, not the data itself. Essentially I need to transpose columns of data to rows of data. The duplicate post suggested was not the problem I'm trying to solve.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • The problem is not the format here, it's the order in which the numbers are written to the file. You will need to order the numbers before writing the files and make a newline when needed. – Jonathan Drapeau Aug 20 '14 at 15:43
  • I am very new to coding. I understand I may have asked my question incorrectly, but any suggestions to help solve my problem would be great rather than just marking it as a duplicate. – user3657527 Aug 20 '14 at 16:00
  • You'll need to include two `StringBuffer`s. One for your H2OIN row and another for your CO2IN row. – Luke Willis Aug 20 '14 at 16:12

2 Answers2

0

I suggest you gather all the values you want on each line in a different List.

So instead, your while loop would look like :

List<String> h2oValues = new ArrayList<String>();
List<String> c02Values = new ArrayList<String>();
while((line = in.readLine()) != null) {
  stream = line.split(parse);
  start = line.substring(6,9);

  if (start.equals("000")) {
    H2OIN = Double.parseDouble(stream[1]);
    CO2IN = Double.parseDouble(stream[2]);

    h2oValues.add(H2OIN);
    c02Values.add(CO2IN);
  }
}

After that, loop the values of h2oValues to write them in a line and do the same for c02Values

for (String value : h2oValues) {
  f.format("%s ", value);
}
// Add a end of line character... using the system one, you might want to change that
f.format(%n);
for (String value : h2oValues) {
  f.format("%s ", c02Values);
}

For the end line, see this question if you want to change it.

Community
  • 1
  • 1
Jonathan Drapeau
  • 2,610
  • 2
  • 26
  • 32
0

You'll need to include two StringBuffers. One for your H2OIN row and another for your CO2IN row.

Like so:

With your other declarations...

StringBuffer H2OINRow = new StringBuffer();
StringBuffer CO2INRow = new StringBuffer();

In your if (start.equals("000")) block...

// in place of the f.format calls
H2OINRow.Append(H2OIN + " ");
CO2INRow.Append(CO2IN + " ");

After your while loops...

f.format("%s\n", H2OINRow);
f.format("%s\n", CO2INRow);
Luke Willis
  • 8,429
  • 4
  • 46
  • 79
  • This puts the values from the input table in the correct order, however the output generates a text file with one long line of data. – user3657527 Aug 20 '14 at 20:11
  • Ah, are you using a windows machine? you may need to use `"%s\r\n"` in your `f.format` calls. There are [OS agnostic solutions](http://stackoverflow.com/questions/207947/java-how-do-i-get-a-platform-independent-new-line-character), but this ought to work for you. – Luke Willis Aug 20 '14 at 20:14
  • Indeed I am. I was able to return the lines with a "f.format("%n"), however the data in each string are not separated by spaces; still one long line. – user3657527 Aug 20 '14 at 20:19
  • @user3657527 really? even with the `+ " "` in each of the `StringBuffer.Append` calls? – Luke Willis Aug 20 '14 at 20:20
  • 1
    That was it! I'd skipped over that. Thanks! – user3657527 Aug 20 '14 at 20:22