• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Problem reading a file

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am reading text file in java using following code



When i execute the code i am getting output as blank text in first line and then all the file details.

file details are :
6
123
3456

output:
blank line
6
123
3456

Anyone can help me with this? I am doing anything wrong in code?
 
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
your program looks ok for me. your input file has any blank line?
 
R Dom
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no it doesnt have any blank line.
 
Bartender
Posts: 11148
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try replacing line 10 with
System.out.println( "'" + line + "'" ); // double-quote quote double-quote
 
Marshal
Posts: 80937
520
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you think that will make any difference? What about a test using the String methods before printing?
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi R Dom,

Could you post the entire code.....?
Might be you are printing some blank space.........

 
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Do you think that will make any difference?


If the blank line does not now equal two single quotes then the blank line comes from somewhere else.
 
Campbell Ritchie
Marshal
Posts: 80937
520
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I missed the single quotes. Sorry. That is what comes from using 3mm tall writing on my screen.
 
R Dom
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my full code


Here is my output from code above:

''
'6'
'2, 3, 5'
'1, 3, 5'
'4'
'1, 3'
'4, 6'
'1, 5'
'p'
'q'
''
'p, q'
'p'

Input file is as follows:
6
2, 3, 5
1, 3, 5
4
1, 3
4, 6
1, 5
p
q

p, q
p

 
anirudh jagithyala
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Dom,

Your code works fine for me..
i get the output as that of the file.
your output is obtained only when there is a space in the file. You could check comparing the output with space.

Please try out with some other files to read...... and i feel its the error is with the file you read.
 
R Dom
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont know why it doesnt work for me.. I dont have any spaces in input file.. I cant attach my txt file..anybody knows how can i attache my input file? this forum doesnt allow me to attach txt files.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In line 24 try defining line like this

 
R Dom
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ken jun wrote:In line 24 try defining line like this



I tried that before it didnt work!
 
ken jun
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your code work properly in my netbean.... Could you tell me what IDE are you using and what's your OS? Have you try run your code in a virtual machine in your PC?
 
R Dom
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ken jun wrote:your code work properly in my netbean.... Could you tell me what IDE are you using and what's your OS? Have you try run your code in a virtual machine in your PC?



I am using Eclipse Helios on Windows 7...
Does it matter? Java is suppose to be platform independent.
 
ken jun
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

R Dom wrote:

ken jun wrote:your code work properly in my netbean.... Could you tell me what IDE are you using and what's your OS? Have you try run your code in a virtual machine in your PC?



I am using Eclipse Helios on Windows 7...
Does it matter? Java is suppose to be platform independent.


I know java is suppose to be platform independent. i just want to know your IDE and OS, it doesn't matter
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic