Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Printing a line of array fields

 
Dan Grindstaff
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this might do the job:



 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12097
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Dan Grindstaff
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, here is the attempt. It creates a file separated by " ". Any comments on how to improve are greatly appreciated. Thanks!

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Jeff!
 
sourav jain
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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[]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic