java.util.InputMismatchException类的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(11.1k)|赞(0)|评价(0)|浏览(91)

本文整理了Java中java.util.InputMismatchException类的一些代码示例,展示了InputMismatchException类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。InputMismatchException类的具体详情如下:
包路径:java.util.InputMismatchException
类名称:InputMismatchException

InputMismatchException介绍

[英]An InputMismatchException is thrown by a scanner to indicate that the next token does not match or is out of range for the type specified in the pattern.
[中]扫描器抛出InputMismatchException,以指示下一个标记与模式中指定的类型不匹配或超出范围。

代码示例

代码示例来源:origin: robovm/robovm

checkOpen();
checkNotNull(pattern);
matchSuccessful = false;
saveCurrentStatus();
if (!setTokenRegion()) {
  recoverPreviousStatus();
  throw new NoSuchElementException();
  recoverPreviousStatus();
  throw new InputMismatchException();

代码示例来源:origin: kevin-wayne/algs4

/**
 * Reads the next token from this input stream, parses it as a {@code boolean}
 * (interpreting either {@code "true"} or {@code "1"} as {@code true},
 * and either {@code "false"} or {@code "0"} as {@code false}).
 *
 * @return the next {@code boolean} in this input stream
 * @throws NoSuchElementException if the input stream is empty
 * @throws InputMismatchException if the next token cannot be parsed as a {@code boolean}
 */
public boolean readBoolean() {
  try {
    String token = readString();
    if ("true".equalsIgnoreCase(token))  return true;
    if ("false".equalsIgnoreCase(token)) return false;
    if ("1".equals(token))               return true;
    if ("0".equals(token))               return false;
    throw new InputMismatchException("attempts to read a 'boolean' value from the input stream, "
                    + "but the next token is \"" + token + "\"");
  }
  catch (NoSuchElementException e) {
    throw new NoSuchElementException("attempts to read a 'boolean' value from the input stream, "
                    + "but no more tokens are available");
  }
}

代码示例来源:origin: kevin-wayne/algs4

/**
 * Reads the next token from standard input, parses it as an integer, and returns the integer.
 *
 * @return the next integer on standard input
 * @throws NoSuchElementException if standard input is empty
 * @throws InputMismatchException if the next token cannot be parsed as an {@code int}
 */
public static int readInt() {
  try {
    return scanner.nextInt();
  }
  catch (InputMismatchException e) {
    String token = scanner.next();
    throw new InputMismatchException("attempts to read an 'int' value from standard input, "
                    + "but the next token is \"" + token + "\"");
  }
  catch (NoSuchElementException e) {
    throw new NoSuchElementException("attemps to read an 'int' value from standard input, "
                    + "but no more tokens are available");
  }
}

代码示例来源:origin: kevin-wayne/algs4

/**
 * Reads the next token from standard input, parses it as a double, and returns the double.
 *
 * @return the next double on standard input
 * @throws NoSuchElementException if standard input is empty
 * @throws InputMismatchException if the next token cannot be parsed as a {@code double}
 */
public static double readDouble() {
  try {
    return scanner.nextDouble();
  }
  catch (InputMismatchException e) {
    String token = scanner.next();
    throw new InputMismatchException("attempts to read a 'double' value from standard input, "
                    + "but the next token is \"" + token + "\"");
  }
  catch (NoSuchElementException e) {
    throw new NoSuchElementException("attempts to read a 'double' value from standard input, "
                    + "but no more tokens are available");
  }
}

代码示例来源:origin: kevin-wayne/algs4

/**
 * Reads the next token from standard input, parses it as a long integer, and returns the long integer.
 *
 * @return the next long integer on standard input
 * @throws NoSuchElementException if standard input is empty
 * @throws InputMismatchException if the next token cannot be parsed as a {@code long}
 */
public static long readLong() {
  try {
    return scanner.nextLong();
  }
  catch (InputMismatchException e) {
    String token = scanner.next();
    throw new InputMismatchException("attempts to read a 'long' value from standard input, "
                    + "but the next token is \"" + token + "\"");
  }
  catch (NoSuchElementException e) {
    throw new NoSuchElementException("attempts to read a 'long' value from standard input, "
                    + "but no more tokens are available");
  }
}

代码示例来源:origin: kevin-wayne/algs4

/**
 * Reads the next token from standard input, parses it as a float, and returns the float.
 *
 * @return the next float on standard input
 * @throws NoSuchElementException if standard input is empty
 * @throws InputMismatchException if the next token cannot be parsed as a {@code float}
 */
public static float readFloat() {
  try {
    return scanner.nextFloat();
  }
  catch (InputMismatchException e) {
    String token = scanner.next();
    throw new InputMismatchException("attempts to read a 'float' value from standard input, "
                    + "but the next token is \"" + token + "\"");
  }
  catch (NoSuchElementException e) {
    throw new NoSuchElementException("attempts to read a 'float' value from standard input, "
                    + "but there no more tokens are available");
  }
}

代码示例来源:origin: kevin-wayne/algs4

/**
 * Reads the next token from standard input, parses it as a byte, and returns the byte.
 *
 * @return the next byte on standard input
 * @throws NoSuchElementException if standard input is empty
 * @throws InputMismatchException if the next token cannot be parsed as a {@code byte}
 */
public static byte readByte() {
  try {
    return scanner.nextByte();
  }
  catch (InputMismatchException e) {
    String token = scanner.next();
    throw new InputMismatchException("attempts to read a 'byte' value from standard input, "
                    + "but the next token is \"" + token + "\"");
  }
  catch (NoSuchElementException e) {
    throw new NoSuchElementException("attempts to read a 'byte' value from standard input, "
                    + "but no more tokens are available");
  }
}

代码示例来源:origin: kevin-wayne/algs4

/**
 * Reads the next token from standard input, parses it as a short integer, and returns the short integer.
 *
 * @return the next short integer on standard input
 * @throws NoSuchElementException if standard input is empty
 * @throws InputMismatchException if the next token cannot be parsed as a {@code short}
 */
public static short readShort() {
  try {
    return scanner.nextShort();
  }
  catch (InputMismatchException e) {
    String token = scanner.next();
    throw new InputMismatchException("attempts to read a 'short' value from standard input, "
                    + "but the next token is \"" + token + "\"");
  }
  catch (NoSuchElementException e) {
    throw new NoSuchElementException("attempts to read a 'short' value from standard input, "
                    + "but no more tokens are available");
  }
}

代码示例来源:origin: stackoverflow.com

int count = 0;
Pattern playerPattern = Pattern.compile("\\w+\\s\\w+(?:,\\w){1,3}");
Scanner fileScan = new Scanner("Sam Slugger,h,h,o,s,w,w,h,w,o,o,o,h,s Jill Jenks,o,o,s,h,h,o,o Will Jones,o,o,w,h,o,o,o,o,w,o,o");
fileScan.useDelimiter("(?<=,\\w)\\s");

while (fileScan.hasNext()){
  String player = fileScan.next();
  Matcher m = playerPattern.matcher(player);
  if (m.find()) {
    player = m.group(0);
  } else {
    throw new InputMismatchException("Players data not in expected format on string: " + player);
  }
  System.out.println(player);
  count++;
}
System.out.printf("%d players found.", count);

代码示例来源:origin: stackoverflow.com

Scanner txtIn = new Scanner(new File("payroll.txt"));
while (txtIn.hasNext()) {
  long employeeNumber;
  String employeeName;
    employeeNumber = txtIn.nextLong();
    employeeName = txtIn.next();
    lastName = txtIn.next();
    hourlyWage = txtIn.nextDouble();
    if (hourlyWage > 10.35) {
      throw new InputMismatchException();
    } else {
      al.add(new Employee(employeeNumber, employeeName,

代码示例来源:origin: stackoverflow.com

Scanner sc = new Scanner(System.in);
String answer = "";
boolean invalidInput = true;    
while(invalidInput){
  try {
    answer = sc.nextLine().toUpperCase();
    if (!answer.equals("A") && !answer.equals("B") && !answer.equals("C")) {
      throw new InputMismatchException();
    } 
    invalidInput = false;
  } catch (InputMismatchException e) {
    System.out.println("Enter a letter please");
    invalidInput = true;
  }
}

代码示例来源:origin: stackoverflow.com

Scanner scanner = new Scanner(System.in);
 System.out.println("3. Quit");
 System.out.println("Please choose a item:");
 mainMenu = scanner.nextInt();
 if (mainMenu == 1){
   System.out.println("Quitting...");
 }else{
   throw new InputMismatchException();
 scanner.nextLine();

代码示例来源:origin: stackoverflow.com

public void choice() throws InputMismatchException {
  Scanner s = new Scanner(System.in);
  int option = 0;

  try {
    option = s.nextInt();
  } catch (InputMismatchException e) {
    throw new InputMismatchException("Enter valid input");
  }

  if (option == 1) {
    System.out.println("e");
  } else if (option == 2) {
    System.out.println("f");
  } else if (option == 3) {
    System.exit(0);
  } else {
    throw new InputMismatchException("Enter valid input");
  }

}

public static void main(String[] args) {
  try {
    new Test().choice();
  } catch (InputMismatchException e) {
    System.out.println(e.getMessage());
  }
}

代码示例来源:origin: stackoverflow.com

private static Scanner kb = new Scanner(System.in);
public static void main(String[] args)
    constant = kb.nextDouble();
      throw new InputMismatchException("Not a double constant. Re-enter");

代码示例来源:origin: stackoverflow.com

try(Scanner in = new Scanner(inputFile))){
      break;
    default:
      throw new InputMismatchException();

代码示例来源:origin: robovm/robovm

checkOpen();
Object obj = cachedNextValue;
cachedNextValue = null;
  return (Byte) obj;
Pattern integerPattern = getIntegerPattern(radix);
String intString = next(integerPattern);
intString = removeLocaleInfo(intString, int.class);
byte byteValue = 0;
  matchSuccessful = false;
  recoverPreviousStatus();
  throw new InputMismatchException();

代码示例来源:origin: robovm/robovm

checkOpen();
Object obj = cachedNextValue;
cachedNextValue = null;
  return (BigDecimal) obj;
Pattern floatPattern = getFloatPattern();
String floatString = next(floatPattern);
floatString = removeLocaleInfoFromFloat(floatString);
BigDecimal bigDecimalValue;
  matchSuccessful = false;
  recoverPreviousStatus();
  throw new InputMismatchException();

代码示例来源:origin: cmusphinx/sphinx4

public void assertToken(String expected, String actual) {
    if (actual.equals(expected))
      return;

    String msg;
    msg = String.format("'%s' expected, '%s' got", expected, actual);
    throw new InputMismatchException(msg);
  }
}

代码示例来源:origin: kevin-wayne/algs4

/**
 * Reads the next token from standard input, parses it as a boolean,
 * and returns the boolean.
 *
 * @return the next boolean on standard input
 * @throws NoSuchElementException if standard input is empty
 * @throws InputMismatchException if the next token cannot be parsed as a {@code boolean}:
 *    {@code true} or {@code 1} for true, and {@code false} or {@code 0} for false,
 *    ignoring case
 */
public static boolean readBoolean() {
  try {
    String token = readString();
    if ("true".equalsIgnoreCase(token))  return true;
    if ("false".equalsIgnoreCase(token)) return false;
    if ("1".equals(token))               return true;
    if ("0".equals(token))               return false;
    throw new InputMismatchException("attempts to read a 'boolean' value from standard input, "
                    + "but the next token is \"" + token + "\"");
  }
  catch (NoSuchElementException e) {
    throw new NoSuchElementException("attempts to read a 'boolean' value from standard input, "
                    + "but no more tokens are available");
  }
}

代码示例来源:origin: kevin-wayne/algs4

/**
 * Reads the next token from this input stream, parses it as a {@code int},
 * and returns the {@code int}.
 *
 * @return the next {@code int} in this input stream
 * @throws NoSuchElementException if the input stream is empty
 * @throws InputMismatchException if the next token cannot be parsed as an {@code int}
 */
public int readInt() {
  try {
    return scanner.nextInt();
  }
  catch (InputMismatchException e) {
    String token = scanner.next();
    throw new InputMismatchException("attempts to read an 'int' value from the input stream, "
                    + "but the next token is \"" + token + "\"");
  }
  catch (NoSuchElementException e) {
    throw new NoSuchElementException("attemps to read an 'int' value from the input stream, "
                    + "but no more tokens are available");
  }
}

相关文章

InputMismatchException类方法