• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with Scanner.nextLine() method  RSS feed

 
Matt Shultz
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I'm reasonably new to programming and need help with the nextLine() method of a scanner I'm using.

This is my code and I realise it may look out of context.


This code is in a for loop and creates a LinkedList of Book objects.
the Scanner is reading form a file via FileReader and the books object is a LinkedList.
The problem I'm having is with the nextLine() call, when I inspect the contents of title via the IDE I'm using it contains an empty String "" but the fName and sName variables contain the strings I expected (being the authors first and second names). So I know the scanner is skipping the line with the title in but it's not returning the line as a string or it's not being stored correctly in the title variable.

The file I'm reading from has the book title on one line and the authors name on the next and this repeats for all the books.

Can anyone help me as this is driving me mad.
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmph. Seems like it should work, eh?

Can you post more code (like the stuff above that block)? I'm pretty new, but I think I can help you.

Janeice
P.S. Welcome to Java Ranch!
 
Matt Shultz
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Janeice

Thanks for your help but I've sorted the problem now.



The file I'm reading from has an integer as the single value of the first line to denote how many books are in the file. I was reading this to set how many times the loop should iterate using nextInt() so when I called nextLine() I was getting the end of the first line.

I feel really stupid and I'm thinking I need a break from staring at my screen!

Thanks for the welcome though

Matt
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a surprisingly common problem. This post (minus the "rant") explains it.
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm glad you found your problem. I thought it might have to do with your file, but took for granted that you said it only had titles and names....


Incidentally, you might be able to get rid of that line if you use a while loop instead of a for loop.... and you could make a variable increment every time there's a title so you can make the app do the work of counting the books.

something like



Have fun!
Janeice
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell --

So what would someone do about a title like 1984? That doesn't look like it will get picked up by the nextLine() method either.....

Janeice
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try it.

A title like 1984 will be picked up as whatever you tell the Scanner it is. If you tell it to look for nextInt() it will find an int and if you tell it to look for next() it will find a String. Look at the post from September I quoted earlier.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!