public static SimpleGraph < String , DefaultEdge > getSimpleGraphFromUser ( ) { Scanner sc = new Scanner ( System . in ) ; SimpleGraph < String , DefaultEdge > graph = new SimpleGraph < String , DefaultEdge > ( DefaultEdge . class ) ; // input number of vertices int verticeAmount ; do { System . out . print ( "Enter the number of vertices (more than one): " ) ; verticeAmount = sc . nextInt ( ) ; } while ( verticeAmount <= 1 ) ; // input vertices for ( int i = 1 ; i <= verticeAmount ; i ++ ) { System . out . print ( "Enter vertex name " + i + ": " ) ; String input = sc . next ( ) ; if ( graph . vertexSet ( ) . contains ( input ) ) { System . err . println ( "vertex with that name already exists" ) ; i -- ; } else { graph . addVertex ( input ) ; } } // input edges System . out . println ( "\nEnter edge (name => name)" ) ; String userWantsToContinue ; do { String e1 , e2 ; do { System . out . print ( "Edge from: " ) ; e1 = sc . next ( ) ; } while ( ! graph . vertexSet ( ) . contains ( e1 ) ) ; do { System . out . print ( "Edge to: " ) ; e2 = sc . next ( ) ; } while ( ! graph . vertexSet ( ) . contains ( e2 ) ) ; graph . addEdge ( e1 , e2 ) ; // add another edge? System . out . print ( "Add more edges? (y/n): " ) ; userWantsToContinue = sc . next ( ) ; } while ( userWantsToContinue . equals ( "y" ) || userWantsToContinue . equals ( "yes" ) || userWantsToContinue . equals ( "1" ) ) ; return graph ; }
Asks the user to input an graph