File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Scanner class help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Scanner class help" Watch "Scanner class help" New topic

Scanner class help

krishna kumar.s

Joined: Oct 01, 2011
Posts: 22
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

Joined: Apr 06, 2010
Posts: 4543

I'm not sure if it's the only problem, but one problem is that you're calling 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

Joined: Oct 01, 2011
Posts: 22

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

Joined: Oct 01, 2011
Posts: 22
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

Joined: Oct 25, 2008
Posts: 2700

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?

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
I agree. Here's the link:
subject: Scanner class help
It's not a secret anymore!