• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

stream output variables to specific positions in output file

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

In a proprietary db editor I put in the following:



which allowed me to line up a variable to a position in the file. I cannot find any such similar Java example. I have ended up "padding" the variables with spaces to mimic the above and what was around 170KB has blossomed into a 1MB file! [So has to be wrong]

I read in from a comma separated file using CSVParser as there are commas within the quotes to a multi-dim array.

Have my outer & inner for loop setup and want to do the Java equivalent. I haven't found any examples like the above code snippet.

Please advise.

Thanks
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the JavaRanch.
Java supports formatted output. Have a look at java.util.Formatter for the details.
 
Peter Brown
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the pointer.

I have



1. I have to put in 2 spaces to "line up" with the particular column rather than saying start values[i][3] at column 31.
2. The file (with 25% done) is now three times the size of the original.

I have made some progress but still not really what I am after....

Thanks
 
Ranch Hand
Posts: 225
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would be a bit more efficient, and arguably more readable, to write:However, that doesn't do anything about the output being different. To help you to get the correct format string, I think we'd need to see some (short) examples of actual data, and code that compiles.
 
Peter Brown
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sample data: There is an "ORD" header Line and then an "ITM" line.

FDP,ORD,182426,"0370","School Corp 1","0409","School 1","9575 N Anywhere Street","","Hope","IN","47246","Joe","Schmow","(812) 123-1234",01/14/2008,11/10/2008,"Delivery",20,11/6/2008,|
FDP,ITM,182426,7,"B027","Cheddar, Redu Fat Shred Y",3,|
FDP,ITM,182426,8,"B430","Macaroni, Elbow",2,|



The other system so you can get some idea of the column layout (for ORD) is:



Thank you,
 
Carey Evans
Ranch Hand
Posts: 225
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That should produce the same output, although it's much less efficient than it could be. However, it looks like there's a problem with your loop on j. With this inner loop, you're formatting each input line several times, identically; 20 times for ORD lines, and nine times for ITM lines. You don't need to loop over i or j, since you only parse one line at a time (i), and you format all the fields at once (j).

Take a step back, and look at your overall design. If you create a new instance of CSVParser from the BufferedReader, and use its getLine() method, it should make more sense because it returns String[]. You can also get better performance by crating a new Formatter instance from the BufferedWriter and using its format() method. Otherwise, CSVParser.parse() creates a new CSVParser for each line, and String.format() creates a new Formatter for each field.

Let me know whether this makes any sense at all.
 
Peter Brown
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Carey for the input - you are right with respect to the multiple formats etc. I could see that with my console output.

I was more concerned with the answer to placing data at a particular point.

What I now have is:



The String [] value = parse.getLine" is wrong as the while reads a line and then this code reads the next line... I can't seem to get away from having to assign a String [] to the parse.getLine(). Please advise.

Thanks,
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can nest an assignment in the while statement. There's an example in the second code sample here (don't dwell on the content of that link too much, it doesn't apply to your case).
[ November 11, 2008: Message edited by: Joe Ess ]
 
Peter Brown
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your help.

Here is the relevant code that now matches the output (and file size) to the required format I was after. [for future reference].

 
We've gotta get close enough to that helmet to pull the choke on it's engine and flood his mind! Or, we could just read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic