aspose file tools*
The moose likes Beginning Java and the fly likes Printing a line of array fields Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Printing a line of array fields" Watch "Printing a line of array fields" New topic
Author

Printing a line of array fields

Dan Grindstaff
Ranch Hand

Joined: Sep 24, 2006
Posts: 138
Hello All, I have the following block of code:



I want to iterate over the nextline object and print each non-null cell on one line instead of hard coding out 20 nextline blocks. Thanks.
Dan Grindstaff
Ranch Hand

Joined: Sep 24, 2006
Posts: 138
I think this might do the job:



fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11422
    
  16

So...what is stopping you?

Generally, it works best around here if you ask an actual question, explaining what exactly you need help with.

-edit-
above was written before I saw your 2nd post.

I think this might do the job:

Why do you just THINK this might do the job? What happened when you ran it?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Dan Grindstaff
Ranch Hand

Joined: Sep 24, 2006
Posts: 138
It prints out the way I need it to, now I just have to get it printed to a file in that format. Thanks.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Dan Grindstaff wrote:now I just have to get it printed to a file in that format.

which format you want to? for example, folder is a file.
Dan Grindstaff
Ranch Hand

Joined: Sep 24, 2006
Posts: 138
I want to print the contents to a new file i.e. txt I will look at the Reader/Writer tools and then post an attempt.
Dan Grindstaff
Ranch Hand

Joined: Sep 24, 2006
Posts: 138
Ok, here is the attempt. It creates a file separated by " ". Any comments on how to improve are greatly appreciated. Thanks!

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

This is not good exception handling:



If you're going to catch an exception, you have to actually handle it or else rethrow it. Just catching it doesn't make the problem go away.

Here you try to initialize reader, and if that fails, you just keep plugging along as if everything were fine, when it clearly isn't--reader is null, meaning when you call reader.readNext() you'll get NPE.

This is why you should not generally initialize local variables to dummy values at declaration just to make the compiler happy. If you didn't initialize reader to null, then this code wouldn't even have compiled, because reader might not have been initialized, which would have been a red flag to you to reexamine your logic, at which point you would have seen that you were trying to continue on using reader after failing to initialize it, which would in turn have led you to just not catch the exception. In general, don't initialize locals at declaration unless you plan to actually use that value.

Also, all close() and similar "clean-up" calls should be in a finally block. (Although, just FYI, one common approach to doing that will lead you to having no choice but to initialize certain variables to null, in apparent contradiction to what I just said above.)
Dan Grindstaff
Ranch Hand

Joined: Sep 24, 2006
Posts: 138
Thank you, Jeff!
sourav jain
Greenhorn

Joined: Nov 19, 2012
Posts: 22

hi ..............

your program cant run becz ,

error at: while(nextline =reader.readnext()!=null)

readnext() return string but nextline is array, tell me how thia will be possible
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
sourav jain wrote: . . .

error at: while(nextline =reader.readnext()!=null)
. . .
Please don’t say things like “becz”.
You have got the quote wrong. You are missing a pair of ().
And you are right; nextline (should be nextLine) should be a String not a String[]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Printing a line of array fields