Example usage for java.util Scanner nextInt

List of usage examples for java.util Scanner nextInt

Introduction

In this page you can find the example usage for java.util Scanner nextInt.

Prototype

public int nextInt() 

Source Link

Document

Scans the next token of the input as an int .

Usage

From source file:fi.jyu.it.ties456.week38.Main.Main.java

public static void createCourse(TeacherRegistry teacher, CourseIS course)
        throws NoSuchTeacherException_Exception {
    Scanner inputID = new Scanner(System.in);
    Scanner cinName = new Scanner(System.in);
    Scanner cinID = new Scanner(System.in);
    Scanner cinCredit = new Scanner(System.in);
    Scanner cinDP = new Scanner(System.in);
    System.out.println("Course Name");
    String cName = cinName.nextLine();
    System.out.println("Search Teacher ID");
    String SID = cinID.nextLine();
    System.out.println(teacher.searchForPerson(SID).get(0).getID());
    System.out.println("Input Teacher ID");
    String ID = inputID.nextLine();
    System.out.println("Course Credit");
    int credit = 0;
    try {/*www. j a v a2s . c om*/
        credit = cinCredit.nextInt();
    } catch (InputMismatchException e) {
        System.err.println("Course credit must be an int");
        main(null);
    }
    System.out.println("Course Description");
    String description = cinDP.nextLine();
    System.out.println(course.createCourse(cName, ID, credit, description));
}

From source file:org.apache.hadoop.hbase.util.ProcessBasedLocalHBaseCluster.java

private static int readPidFromFile(String pidFile) throws IOException {
    Scanner scanner = new Scanner(new File(pidFile));
    try {//from  w  w w  . j av a  2s .c  om
        return scanner.nextInt();
    } finally {
        scanner.close();
    }
}

From source file:com.bright.json.JSonRequestor.java

public static void chkVersion(String cmURL, List<Cookie> cookies) {
    // Logout and purge cookie on server
    cmMain mainreq = new cmMain();
    mainreq.setService("cmmain");
    mainreq.setCall("getVersion");

    Gson g = new Gson();
    String json = g.toJson(mainreq);

    String ver_string = doRequest(json, cmURL, cookies);
    cmgetVersion getVer = new Gson().fromJson(ver_string, cmgetVersion.class);
    System.out.println("Version JSON String " + ver_string);
    String message = getVer.getCmdaemonBuild().toString();
    @SuppressWarnings("resource")
    Scanner resInt = new Scanner(message).useDelimiter("[^0-9]+");
    int build_ver = resInt.nextInt();
    if (build_ver < Constants.CMDAEM0N_MIN_BUILD) {
        JOptionPane.showMessageDialog(null,
                "You need CMDaemon revision " + Constants.CMDAEM0N_MIN_BUILD + " or later.");
        System.out.println("You need CMDaemon revision " + Constants.CMDAEM0N_MIN_BUILD + " or later.");
        System.exit(0);/* w  ww .  j a v  a 2s.  c  o  m*/
        System.out.println("Succesfully Logged Off");
    }

}

From source file:Main.java

private static int getChoice() {

    Scanner input = new Scanner(System.in);
    int choice = 1; //default value to force skip of first if statement check
    do {/*from w  ww  . ja v a 2  s. c om*/
        if (choice < 1 | choice > 4)
            System.out.println("Invalid choice...");
        System.out.print("Choice: ");
        /*This while loop checks the buffer stream for the next incoming input.
         *the purpose is to check if the input can be assigned an int value.
         *at this time choice has not be assigned*/
        while (!input.hasNextInt()) { // ask for an input, if input NOT nextInt then:
            input.next(); // consumes the token and returns to top of loop for another token
            System.out.print("Try again: "); //don't want println as that would shift cursor to new line
        }
        choice = input.nextInt(); //once the input has been found to be an int, assign to choice
        /*now choice needs to be checked for the correct range of values.
         * if its not within 1-4 then repeat while loop above*/
    } while (choice < 1 | choice > 4);
    //input.close();
    return choice;
}

From source file:com.wandrell.example.swss.client.console.ConsoleClient.java

/**
 * Returns an integer value read from the input.
 * <p>//ww  w . j av a2s. c  o  m
 * This will try to parse an integer until one is found, rejecting all the
 * invalid lines.
 *
 * @param scanner
 *            scanner for the input
 * @return an integer read from the input
 */
private static final Integer getInteger(final Scanner scanner) {
    Integer integer; // Parsed integer
    Boolean valid; // Status flag for the loop

    valid = false;
    integer = null;
    while (!valid) {
        // Runs until an integer is found
        valid = scanner.hasNextInt();
        if (valid) {
            // Found an integer
            integer = scanner.nextInt();
        } else {
            // The line is not an integer
            // It is rejected
            scanner.nextLine();
        }
    }

    return integer;
}

From source file:gov.nasa.jpl.memex.pooledtimeseries.PoT.java

public static double[][] loadTimeSeries(Path filename) {
      double[][] series = new double[1][1];

      try (InputStream in = Files.newInputStream(filename);
              BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
          Scanner scin = new Scanner(in);

          int num_frames = scin.nextInt();
          int dim = scin.nextInt();

          series = new double[num_frames][dim];

          for (int i = 0; i < num_frames; i++) {
              for (int j = 0; j < dim; j++) {
                  series[i][j] = scin.nextDouble();
              }// www .  j  a  va2  s.  c o  m
          }
      } catch (IOException e) {
          e.printStackTrace();
      }

      return series;
  }

From source file:org.pooledtimeseries.PoT.java

public static double[][] loadTimeSeries(Scanner scin) {
    double[][] series = new double[1][1];
    int num_frames = scin.nextInt();
    int dim = scin.nextInt();

    series = new double[num_frames][dim];

    for (int i = 0; i < num_frames; i++) {
        for (int j = 0; j < dim; j++) {
            series[i][j] = scin.nextDouble();
        }//from  ww  w .j a va2  s  .  com
    }
    scin.close();

    return series;
}

From source file:simcommunity.TheSim.java

public static void batchSim_completa() {

    Scanner valore = new Scanner(System.in).useLocale(Locale.US);

    double o; //omogeneit

    System.out.print("\n===================================");
    System.out.print("\nSIMULAZIONE BATCH completa\n");
    System.out.print("\n===================================");

    System.out.print("\n\nNumero individui iniziale ");
    int comIniziali = valore.nextInt();

    System.out.print("Numero individui  finale ");
    int comFinale = valore.nextInt();

    System.out.print("Incremento individui ");
    int incrementocomunita = valore.nextInt();

    System.out.print("Numero collegamenti iniziale ");
    int colIniziali = valore.nextInt();

    System.out.print("Numero collegamenti finale ");
    int colFinali = valore.nextInt();

    System.out.print("Incremento collegamenti ");
    int incrementocollegamenti = valore.nextInt();

    System.out.print("Dimensione del DNA ");
    GlobalVar.dimDNA = valore.nextInt();

    System.out.print("Soglia ere massima ");
    GlobalVar.numERE = valore.nextInt();

    System.out.print("Soglia di omogeneit ");
    GlobalVar.omoSoglia = valore.nextDouble();

    //modifica/* ww  w  .j  a v  a  2 s.  c  o  m*/
    //System.out.println("\nind\tcon\tere\tomo");

    //Riga degli individui
    for (int i = comIniziali; i <= comFinale; i = i + incrementocomunita)
        System.out.print("\t" + i);
    System.out.println();

    for (int j = colIniziali; j <= colFinali; j = j + incrementocollegamenti) {
        System.out.print(j + "\t");
        for (int i = comIniziali; i <= comFinale; i = i + incrementocomunita) {
            GlobalVar.dimensionecom = i;
            GlobalVar.numcollegamenti = j;

            o = ciclocomunita();

            System.out.print(GlobalVar.ereEffettuate + "\t");
        }
        System.out.println();

    }

}

From source file:simcommunity.TheSim.java

public static void batchSim_fixedconnessioni() {

    Scanner valore = new Scanner(System.in).useLocale(Locale.US);

    double o; //omogeneit

    System.out.print("\n===================================");
    System.out.print("\nSIMULAZIONE BATCH con individui crescenti e numero di connessioni fisse\n");
    System.out.print("\n===================================");

    System.out.print("\n\nDimensione della comunit iniziale ");
    int comIniziali = valore.nextInt();

    System.out.print("Dimensione della comunit finale ");
    int comFinale = valore.nextInt();

    System.out.print("Incremento comunit ");
    int incrementocomunita = valore.nextInt();

    System.out.print("Numero collegamenti ");
    GlobalVar.numcollegamenti = valore.nextInt();

    System.out.print("Dimensione del DNA ");
    GlobalVar.dimDNA = valore.nextInt();

    System.out.print("Quante ere ");
    GlobalVar.numERE = valore.nextInt();

    System.out.print("Soglia di omogeneit ");
    GlobalVar.omoSoglia = valore.nextDouble();

    System.out.println("\nINDIVIDUI\tCONNESSIONI\tCICLI\tOMOGENEIT");

    for (int i = comIniziali; i < comFinale; i = i + incrementocomunita) {
        GlobalVar.dimensionecom = i;/*from ww w.jav  a 2  s .c  o  m*/
        o = ciclocomunita();

        System.out.println(
                "\n" + i + "\t\t" + GlobalVar.numcollegamenti + "\t\t" + GlobalVar.ereEffettuate + "\t\t" + o);

    }

}

From source file:simcommunity.TheSim.java

public static void batchSim_completa_percentuali() {

    Scanner valore = new Scanner(System.in).useLocale(Locale.US);

    double o; //omogeneit

    System.out.print("\n===================================");
    System.out.print("\nSIMULAZIONE BATCH completa con collegamenti espressi in percentuali\n");
    System.out.print("\n===================================");

    System.out.print("\n\nNumero individui iniziale ");
    int comIniziali = valore.nextInt();

    System.out.print("Numero individui finale ");
    int comFinale = valore.nextInt();

    System.out.print("Incremento individui ");
    int incrementocomunita = valore.nextInt();

    System.out.print("Percentuale (sul numero di individui) di collegamenti iniziale ");
    int pcolIniziali = valore.nextInt();

    System.out.print("Percentuale (sul numero di individui) di collegamenti finale ");
    int pcolFinali = valore.nextInt();

    System.out.print("Incremento percentuale dei collegamenti ");
    int pincrementocollegamenti = valore.nextInt();

    System.out.print("Dimensione del DNA (meglio multipli di 9) ");
    GlobalVar.dimDNA = valore.nextInt();

    System.out.print(// ww  w .j  a v  a  2 s  .c  o  m
            "Soglia ere massima (limite di cicli oltre i quali il tentativo di omogeneizzazione termina ");
    GlobalVar.numERE = valore.nextInt();

    System.out.print("Soglia di omogeneit ");
    GlobalVar.omoSoglia = valore.nextDouble();

    //modifica
    //System.out.println("\nind\tcon\tere\tomo");

    //Riga degli individui
    for (int i = comIniziali; i <= comFinale; i = i + incrementocomunita)
        System.out.print("\t" + i);
    System.out.println("\tnumero membri della comunit");

    for (int j = pcolIniziali; j <= pcolFinali; j = j + pincrementocollegamenti) {
        System.out.print(j + "%\t");
        for (int i = comIniziali; i <= comFinale; i = i + incrementocomunita) {
            GlobalVar.dimensionecom = i;
            GlobalVar.numcollegamenti = Math.round(j * i / 100); //il j% della comunit i deve essere connessa

            o = ciclocomunita();

            System.out.print(GlobalVar.ereEffettuate + "\t");
        }
        System.out.println();

    }

}