• Post Reply Bookmark Topic Watch Topic
  • New Topic

Not reading all lines while comparing user entered name to the one that exists in file  RSS feed

 
h patel
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is my code:

Logic her eis when user selects option1, it asks for user to enter name and as soon as user enters name, it should compar name to existing names in txt file. I have user while loop and for loop but for some reason, it doesnt compare properly as there seems to be some mistake in looping and it just read first line or you can say single line rather than comparing it with all lines in txt file. Can you help me to overcome problem with looping ? i have attached votes.txt file with this. Also, if user doesnt exists infile, it will ask user to enter vote as yes or no and add it to file and then count total number of Yes and No votes from file and compare them. Would you please help me with logic in that too?. I am completely new to this java

my votes.txt file looks as below with two columns namely (name and vote).

Hiren No
samir yes
bob no
rikul no
master yes
patrick no
summer yes
bhanja no







 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please first edit your post and format your code.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never be afraid to put dozens of System.out.println statements in to see what your code is REALLY doing. if i'm putting them in for debugging purposes, i'll purposely not indent them at all so they are easy to find/comment out later.
 
h patel
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dont really know asto what you are saying . I have tried with inserting println statements and tried printing line too but then it just prints off 1 line.
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Harry wrote:Please first edit your post and format your code.
I have done it for OP, who has never used code tags before, also remoivng many unnecessary blank lines; they usually make the code look much better, but here the formatting was poor.
 
h patel
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Campbell,

I have tried to reformat it . Hope it helps in overcoming the problem.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
h patel wrote:Dont really know asto what you are saying . I have tried with inserting println statements and tried printing line too but then it just prints off 1 line.

What I would suggest you do is put in print statement to see what the variables are. What does s.hasNext() return each time? What is "line" set to each time?

Quite frankly, what I'd really do is throw most of this code away. There is no reason to have ANY other code if your loop to reach each line doesn't work. right now, it's just noise. Dump everything but the bare minimum you need to read and print each line. I'd make the while loop this:


or whatever it needs to be to print the entire file. Then add your code back in a few lines at a time.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
h patel wrote:Below is my code:

h patel,

Please DontWriteLongLines. It makes your thread very hard to read, and it's actually bad coding practice.
I've broken yours up as best I can by shortening your "divider" lines, but it's still too wide. Please read the linked page COMPLETELY, and use the 'Edit' key to change your post.

And for future reference, please remember:
80 characters max.
(the SSCCE page actually recommends 62)
And that includes string literals AND comments AND long method calls.

Oh, and you're usually better off using spaces for indentation, rather than TABs.

Thanks.

Winston
 
h patel
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for putting in your efforts. I have manage to solve it :-)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!