• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

scanner class's object throwing java.util.NoSuchElementException

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been implementing the graph algorithms in java.
i was trying to do it in elaborate way as they do in java.
inputting graph from the file and then doing bfs on it.
here is my code that in java.

TraversalUtils.java


TraversalUtilTests.java



can anyone please tell me why i am getting it like this on running TraversalUtilsTest.java.
in the while loop, i am trying to run bfs again and again from node 1 of different graphs.
It do not seem logical to me at all
Please help..
This is the input output that i have faced

Enter file code : 1
1
2
3
Enter file code : Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at graph.GraphMaker.getGraph(GraphMaker.java:30)
at graph.TraversalUtilsTest.main(TraversalUtilsTest.java:10)

just in case you need, the other files are these.

Graph.java



GraphMaker.java




also, graph1.txt is file like this.
3
1 2
2 3
3 1

Thank you very much.
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are using hasNext(), which tells you there is one token to find. Then you are trying to read two tokens. When you get to the last token, you will try to read the token one beyond last, which doesn't exist.

Hence: no such element exception.
 
amit kumararya
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry but i could not follow you.

The Graphmaker has hasNext().
But they are reading from Graph1.txt, and i have formatted it in such a way that when ever there will be next available. we will have two tokens in the next line.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic