Java Scanner Usage selectProject(List projectList)

Here you can find the source of selectProject(List projectList)

Description

select Project

License

Open Source License

Declaration

public static String selectProject(List<String> projectList) 

Method Source Code

//package com.java2s;
/*/*from  w w  w.  java 2 s . c om*/
 * Copyright (C) 2012 Raymond Rusk
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

import java.util.List;

import java.util.Scanner;

public class Main {
    public static String selectProject(List<String> projectList) {

        if (projectList == null || projectList.isEmpty()) {
            return null;
        }

        System.out.println("Gerrit Projects:");
        for (int i = 0; i < projectList.size(); i++) {
            System.out.println("[" + i + "] " + projectList.get(i));
        }

        // Ask user for project to be removed from Gerrit
        System.out.print("Number of project to be deleted? [-1 to quit] ");
        Scanner scan = new Scanner(System.in);
        String input = scan.nextLine();
        int num = -1;
        try {
            num = Integer.parseInt(input);
        } catch (NumberFormatException ne) {
            System.out.println("You must enter an integer between 0 and "
                    + (projectList.size() - 1));
        }
        if (num == -1) {
            return null;
        }
        if (num < -1 || num > projectList.size() - 1) {
            System.out.println("Invalid project number.  Exiting...");
            return null;
        }

        // XXX this method doesn't seem to actually remove the project, so 
        // having this message printed here is a little bit misleading.
        System.out
                .print("Are you positive that you want to remove project '["
                        + num + "] " + projectList.get(num) + "'? [y/N] ");
        input = scan.nextLine();
        if (input.equalsIgnoreCase("y")) {
            return projectList.get(num);
        }
        return null;
    }
}

Related

  1. queryMenu(Scanner in, String msg, LinkedList options)
  2. queryStr(Scanner in, String msg)
  3. removeFirstNLines(String text, int n)
  4. removeIndexes(String xpath)
  5. removeNLinesForwardsFromStringMatch(String text, String match, int n)
  6. splitLines(String string)
  7. splitToDouble(String input)
  8. stringToArray(String str)
  9. stringToDoubleList(String s)