• 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

Printing a line of array fields

 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this might do the job:



 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Jeff!
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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[]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic