Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-06-2008, 08:20 AM
Member
 
Join Date: Apr 2008
Posts: 3
aytidaalkuhs is on a distinguished road
[SOLVED] file i/o problem
Hi.
I was writing a small code to facilitate text file reading and used the following code to read and display the text file.


while(handle.read()!= -1)
{
System.out.print((char)handle.read());
}
where handle is an object of FileInputStream class.
What it does is print the alternate charcters in the file starting from the second character.
Why is it behaving in such a manner? Please throw some light.

Alternatively, i used a code snippet from java 2 complete reference which is working just fine.

What i want to know is whats the problem with my logic or code and not interested in getting any new ways to do it cuz i already have a correct way to do it at hand.

Waiting..
aytidaalkuhs.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-06-2008, 11:52 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 740
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Welcome to the forums aytidaalkuhs!

Please remember to use [ code] [ /code] tags when posting code.

It's printing alternating characters from the second on because you made one call from within the while line, and another during the output line. See the API for more details. Take a view at the code too:
Code:
// read reads from the input stream while(handle.read()!= -1) { // with this, you've made one read here, comparing every other read to -1 // with this, you've made another read, outputting every other character System.out.print((char)handle.read()); }
My suggestion is you need a new way to do it, because if your intention is to print every char in the stream- well, this code just isn't working, now is it?
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on July 13, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-06-2008, 07:17 PM
Member
 
Join Date: Apr 2008
Posts: 3
aytidaalkuhs is on a distinguished road
thanks
thank you captain for the solution.
actually, after coding for abt 2 hrs as a noob(in java), i couldnt even see that i was calling the handle.read() twice. Really, i can be dumb at times.
Thanks anyways and ill keep in mind to use <code> </code> in all later posts.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-06-2008, 07:42 PM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 740
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
No problem. We all have our moments. I've stared at code for hours on end and all the while I couldn't actually see the = when it should have been ==

__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on July 13, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] alignment problem nanimtech JavaServer Pages (JSP) and JSTL 1 04-10-2008 02:23 PM
[SOLVED] Actionevent problem Cymro New To Java 3 04-04-2008 08:11 AM
[SOLVED] Reading a text file into an Array DonCash New To Java 6 04-01-2008 01:18 PM
[SOLVED] getting values from a text file dav9999 New To Java 8 04-01-2008 02:51 AM
[SOLVED] Delete Current line from file Azndaddy New To Java 1 03-28-2008 01:37 PM


All times are GMT +3. The time now is 11:05 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org