List of usage examples for java.util InputMismatchException InputMismatchException
public InputMismatchException()
From source file:by.bsu.zmiecer.PieChartDemo1.java
/** * Creates a sample dataset./*w ww .j av a 2s .c o m*/ * * Source: http://www.bbc.co.uk/news/business-15489523 * * @return A sample dataset. */ private PieDataset createDataset() { DefaultPieDataset dataset = new DefaultPieDataset(); try { File file = new File( "C:\\Users\\\\\\GitHub\\BSU\\2 course\\Practical Training\\Week 5\\Task53\\input.txt"); Scanner sc = new Scanner(file); while (sc.hasNext()) { String name; if (sc.hasNext()) name = sc.next(); else throw new InputMismatchException(); Double val; if (sc.hasNext()) { val = Double.valueOf(sc.next()); if (val < 0) throw new NumberFormatException(); } else throw new InputMismatchException(); dataset.setValue(name, val); } } catch (FileNotFoundException e) { JOptionPane.showMessageDialog(getParent(), "File not found!"); } catch (InputMismatchException e) { JOptionPane.showMessageDialog(getParent(), "Enter corret data!"); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(getParent(), "Enter corret data!"); } return dataset; }
From source file:com.okta.tools.awscli.java
private static int numSelection(int max) { Scanner scanner = new Scanner(System.in); int selection = -1; while (selection == -1) { //prompt user for selection System.out.print("Selection: "); String selectInput = scanner.nextLine(); try {/*from w ww. ja v a 2s . c om*/ selection = Integer.parseInt(selectInput) - 1; if (selection >= max) { InputMismatchException e = new InputMismatchException(); throw e; } } catch (InputMismatchException e) { //raised by something other than a number entered logger.error("Invalid input: Please enter a number corresponding to a role \n"); selection = -1; } catch (NumberFormatException e) { //raised by number too high or low selected logger.error("Invalid input: Please enter in a number \n"); selection = -1; } } return selection; }