I'm builing on a Java API for MongoDB. More specific, right now I'm trying to encapsulate the process of connecting to MongoDB and choosing a database and a collection from that database to it's own class in a single method.
package mongoDB;
import java.net.UnknownHostException;
import java.util.*;
import com.mongodb.*;
public class MongoDBConnector {
public DBCollection mongoEasy (String hostname, int port) {
try {
Mongo m = new Mongo(hostname, port);
//choose database
List<String> databaseNames = m.getDatabaseNames();
System.out.println("Choose database: " + databaseNames.toString());
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
if(!databaseNames.contains(input.toString())){
System.out.println("Database does not exist.");
System.exit(1);
}
DB db = m.getDB(input.toString());
//choose collection
Set<String> collectionNames = db.getCollectionNames();
System.out.println("Choose collection " + collectionNames.toString());
input = scanner.nextLine();
if(!collectionNames.contains(input.toString())){
System.out.println("collection does not exist");
System.exit(1);
}
DBCollection dbCollection = db.getCollection(input.toString());
scanner.close();
return dbCollection;
} catch (UnknownHostException e) {
System.out.println("Connection failed.");
System.exit(1);
} catch(MongoException e){
System.out.println("Fail.");
System.exit(1);
} finally{
}
return null;
}
}
If i create an instance of MongoDBConnector and call the mongoEasy a single time within a main method everything is fine. However if i call the method a second time it pumps out a NoSuchElementException. Take this example: package mongoDB;
import com.mongodb.*;
import mongoDB.MongoDBConnector;
public class MongoDBOperations2 {
public static void main(String[] args) {
MongoDBConnector mongoDBConnector = new MongoDBConnector();
DBCollection collection = mongoDBConnector.mongoEasy("localhost", 27017);
collection = mongoDBConnector.mongoEasy("localhost", 27017);
}
}
Any time I'm trying to execute this I get (I deleted the database and collection names and line number for main method doesn't fit because i simplefied it for the question):
Choose database [...]
test
Choose collection [...]
test
Choose database [local, StudiumUlm, test]
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at mongoDB.MongoDBConnector.mongoEasy(MongoDBConnector.java:18)
at mongoDB.MongoDBOperations2.main(MongoDBOperations2.java:)
The second time I call the method the scanner is not waiting for my input. Any suggestions?
Thanks. Cheers, Magnus
According to the Scanner.close() documentation, it will close the underlying Reader/Stream if it implements Closeable .
Since your code closes the Scanner, you can't read from System.in after the first time(which is exactly what you are seeing).
Why not add a close method to your MongoDBConnector like :
public class MongoDBConnector {
Scanner scanner;
public MongoDBConnector() {
this.scanner = new Scanner(System.in);
}
public void close(){
this.scanner.close();
}
// the rest of your code but don't create a new scanner or close it
}
Then in your mainline do :
public class MongoDBOperations2 {
public static void main(String[] args) {
MongoDBConnector mongoDBConnector = new MongoDBConnector();
try {
DBCollection collection = mongoDBConnector.mongoEasy("localhost", 27017);
collection = mongoDBConnector.mongoEasy("localhost", 27017);
}finally {
mongoDBConnector.close();
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.