This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Scanner class help

 
krishna kumar.s
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

My aim is to read a content of the file , it contains "Alien","colin" first name of 5200 persons with in quotes and seperated by coma. I want to read the file and parse each name .

i used below programme




the null alone is stored in array after 2113 and exception is thrown ,please help me.

[Edit - added code tags - MB]
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure if it's the only problem, but one problem is that you're calling s.next() twice each time round the loop. Each call to next() moves on to the next token, but that isn't what you want here. Change the print line to System.out.println(a[i]) and you'll avoid that.

If there's still a problem after that, it's much more useful if you tell us what line the exception is thrown on.
 
krishna kumar.s
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.util.NoSuchElementException

Actually the file contains 5200 names with comma seperated . my aim is to seperate each name and store it in a array.
I use system.println to see how the seperator works , if i not using the string array all 5200 names are printed , if try to transfer to array and exception is thrown.
 
krishna kumar.s
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Matthew Brown ,

Yes , you were correct . When i removed the s.Next(). it resolved the issue , twice i was calling . Cool thanks a lot.

That is the diffrence between an experienced programmer and begginer. Thanks for your time.


my final code is , this is to solve the problem 22 of project euler. it took 198 ms to execute ( too long ) i need to learn a lot. using methods and reusing of codes I have to learn.
is there any book that will help me?
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using so many if statements? Isn't there some kind of logic that can be implemented in a couple of lines of code?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic