List of usage examples for java.util Scanner nextInt
public int nextInt()
From source file:TextFileTest.java
/** * Reads an array of employees from a scanner * @param in the scanner/*w w w. j a v a 2 s .co m*/ * @return the array of employees */ private static Employee[] readData(Scanner in) { // retrieve the array size int n = in.nextInt(); in.nextLine(); // consume newline Employee[] employees = new Employee[n]; for (int i = 0; i < n; i++) { employees[i] = new Employee(); employees[i].readData(in); } return employees; }
From source file:Matrix_Operations.java
public static int getRowsNumberFromUser() { Scanner keyboard = new Scanner(System.in); //Allow user to enter number of rows int rows;/*from ww w .j a v a 2 s . co m*/ System.out.print("Enter rows: "); rows = keyboard.nextInt(); return rows; }
From source file:Matrix_Operations.java
public static int getColumnNumberFromUser() { Scanner keyboard = new Scanner(System.in); //Allow user to enter number of columns int columns;/*w ww . j a v a 2s. c om*/ System.out.print("Enter columns: "); columns = keyboard.nextInt(); return columns; }
From source file:formatMessage.VerifyInputScanner.java
public static int verifyInt() { while (true) { Scanner input = new Scanner(System.in); try {//from w w w . java 2 s . com int verified = input.nextInt(); return verified; } catch (InputMismatchException e) { System.out.println("Geen geldig nummer. Probeer opnieuw."); } } }
From source file:jfractus.app.Main.java
private static void parseSize(String sizeStr, Dimension dim) throws ArgumentParseException, BadValueOfArgumentException { Scanner scanner = new Scanner(sizeStr); scanner.useLocale(Locale.ENGLISH); scanner.useDelimiter("x"); int outWidth = 0, outHeight = 0; try {/* www . j a v a 2s.com*/ outWidth = scanner.nextInt(); outHeight = scanner.nextInt(); if (outWidth < 0 || outHeight < 0) throw new BadValueOfArgumentException("Bad value of argument"); } catch (InputMismatchException e) { throw new ArgumentParseException("Command line parse exception"); } catch (NoSuchElementException e) { throw new ArgumentParseException("Command line parse exception"); } if (scanner.hasNext()) throw new ArgumentParseException("Command line parse exception"); dim.setSize(outWidth, outHeight); }
From source file:org.wise.WISE.java
private static int mainMenuPrompt() { Scanner reader = new Scanner(System.in); System.out.println("\nAvailable actions:\n" + "[0] Reset WISE (database & curriculum) to initial state\n" + "[1] Exit\n" + "Enter number:"); try {//www . j a v a2 s . co m //get user input return reader.nextInt(); } catch (InputMismatchException ime) { return mainMenuPrompt(); } }
From source file:IO.java
public static int[] readIntVec(File file, int nData) { int[] data = new int[nData]; try {/* w w w .ja v a 2 s .c om*/ Scanner sc = new Scanner(file); sc.useDelimiter(",|\\n"); for (int i = 0; i < nData; i++) { data[i] = sc.nextInt(); } } catch (FileNotFoundException e) { e.printStackTrace(); } return data; }
From source file:org.apache.bookkeeper.bookie.FileSystemUpgrade.java
private static int detectPreviousVersion(File directory) throws IOException { String[] files = directory.list(BOOKIE_FILES_FILTER); File v2versionFile = new File(directory, BookKeeperConstants.VERSION_FILENAME); if ((files == null || files.length == 0) && !v2versionFile.exists()) { // no old data, so we're ok return Cookie.CURRENT_COOKIE_LAYOUT_VERSION; }/* w w w .j a v a 2 s. c o m*/ if (!v2versionFile.exists()) { return 1; } Scanner s = new Scanner(v2versionFile, UTF_8.name()); try { return s.nextInt(); } catch (NoSuchElementException nse) { LOG.error("Couldn't parse version file " + v2versionFile, nse); throw new IOException("Couldn't parse version file", nse); } catch (IllegalStateException ise) { LOG.error("Error reading file " + v2versionFile, ise); throw new IOException("Error reading version file", ise); } finally { s.close(); } }
From source file:testing.test.java
private static void testIFSExtraction() { String[] coordIndexArray = null; String coordIndex = "0 1 2 3 -1"; String points = "-1 -1 1 1 -1 1 1 1 1 -1 1 1 1 1 -1 -1 1 -1 -1 -1 -1 1 -1 -1"; List totalPointParts = null;/*from w w w. j a va 2s . co m*/ coordIndex = coordIndex.replaceAll("\\r|\\n", " ").trim().replaceAll(" +", " ").replaceAll(",", ""); System.out.println(coordIndex); points = points.replaceAll("\\r|\\n", " ").trim().replaceAll(" +", " ").replaceAll(",", ""); System.out.println(points); Scanner sc = new Scanner(coordIndex).useDelimiter(" "); int maxCoordIndex = 0; while (sc.hasNextInt()) { int thisVal = sc.nextInt(); if (thisVal > maxCoordIndex) { maxCoordIndex = thisVal; } } System.out.println("maxCoordIndex: " + maxCoordIndex); totalPointParts = new ArrayList(); totalPointParts = getPointParts(points, maxCoordIndex); if (coordIndex.contains("-1")) { coordIndex = coordIndex.substring(0, coordIndex.lastIndexOf("-1")); coordIndexArray = coordIndex.split(" -1 "); } else { coordIndexArray = new String[1]; coordIndexArray[0] = coordIndex; } System.out.println("coordIndexArray"); System.out.println("coordIndexArray.length: " + coordIndexArray.length); for (int i = 0; i < coordIndexArray.length; i++) { System.out.println(i + ": " + coordIndexArray[i]); } System.out.println("totalPointsParts"); for (int j = 0; j < totalPointParts.size(); j++) { System.out.println(j + ": " + totalPointParts.get(j)); } }
From source file:MainProgram.MainProgram.java
public static void registrationMain(String courseNumber) throws InterruptedException, IOException { System.out.println("Web driver starts..."); String passwordString = String.copyValueOf(MainFrameProgram.pennPassField.getPassword()); int semesterNum = Integer.parseInt(MainFrameProgram.semesterComboBox.getSelectedItem().toString()); WebDriver_Registration.courseRegistration(MainFrameProgram.pennIDTextField.getText(), passwordString, courseNumber, semesterNum, MainFrameProgram.dropCheckBox.isSelected()); Scanner sc = new Scanner(System.in); int i = sc.nextInt(); }