• Post Reply Bookmark Topic Watch Topic
  • New Topic

scanner class's object throwing java.util.NoSuchElementException  RSS feed

 
amit kumararya
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!