本文整理汇总了Java中java.util.InputMismatchException类的典型用法代码示例。如果您正苦于以下问题:Java InputMismatchException类的具体用法?Java InputMismatchException怎么用?Java InputMismatchException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
InputMismatchException类属于java.util包,在下文中一共展示了InputMismatchException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import java.util.InputMismatchException; //导入依赖的package包/类
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int i;
try {
System.out.print("enter an int => ");
i = s.nextInt();
} catch (InputMismatchException e) {
System.out.println("Error! " + e);
i = -1;
} finally {
s.close();
System.out.println("we're done");
}
System.out.println("What is the value of i: " + i);
}
示例2: ask
import java.util.InputMismatchException; //导入依赖的package包/类
/**
* Prompt the user to enter a number and return it
* pre: none
* post: Question parameter printed, user integer input returned
*/
public int ask(String question) {
badInput = true;
do {
try {
System.out.print(question);
int next = input.nextInt();
if (next > 0) {
badInput = false;
return next;
} else badInput = true;
} catch (InputMismatchException e) {
System.out.println("Please input a positive whole number.");
}
input.nextLine();
} while (badInput);
badInput = true;
System.out.println("ERROR OCCURRED. You shouldn't see this message. Error code: 100");
return 100;
}
示例3: filteringBy
import java.util.InputMismatchException; //导入依赖的package包/类
/**
* This method ask the user to input the place.
* @param array.
* @exception InputMismatchException : Error on the input, try again.
*/
@SuppressWarnings("resource")
public void filteringBy(ArrayList<Wifi> array) throws InputException {
try {
System.out.println("Input an latitude please :");
double pointLatitude = new Scanner(System.in).nextDouble();
System.out.println("Input an longitude please :");
double pointLongitude = new Scanner(System.in).nextDouble();
System.out.println("Input a radius please (meters) :");
double radius = new Scanner(System.in).nextDouble();
if(checkLatitude(pointLatitude) && checkLongitude(pointLongitude) && radius >= 0) {
EarthCoordinate pointLocation = new EarthCoordinate(pointLongitude, pointLatitude, 0.0); // Latitude 0.0 by default.
new WriteKmlPlace(array, pointLocation, radius);
}
else throw new InputException("There is ne such latitude/longitude/radius.");
}
catch (InputMismatchException ex) {
System.out.println("Error on the input, try again.");
filteringBy(array);
}
}
示例4: getInt
import java.util.InputMismatchException; //导入依赖的package包/类
public static int getInt(Scanner input) throws InputMismatchException{
int value = -1;
boolean bad = true;
do
{
try {
value = input.nextInt();
if (value != -1 && input.nextLine().equals(""))
bad = false;
else
System.out.println("Please enter only one integer");
} catch (InputMismatchException ex) {
System.out.println("Incorrect entry. Please input only a positive integer.");
input.nextLine();
}
} while(bad);
return value;
}
示例5: getCommand
import java.util.InputMismatchException; //导入依赖的package包/类
/**
* Prompts a user to select a command from a list of commands.
* @param thePrompt the prompt
* @param theCommands the list of commands
* @return the selected command
* @throws NullPointerException if the prompt or command array is null
*/
protected Command getCommand(final String thePrompt, final Command[] theCommands) {
displayNumberedList(Arrays.stream(Objects.requireNonNull(theCommands)).map(Object::toString).toArray(String[]::new));
try {
int commandNumber = getInteger(Objects.requireNonNull(thePrompt), 1, theCommands.length);
if(commandNumber > 0 && commandNumber <= theCommands.length) {
return theCommands[commandNumber - 1];
} else {
return null;
}
} catch(final InputMismatchException e) {
print("Command not recognized.");
return getCommand(thePrompt, theCommands);
}
}
示例6: validateOtpMMode
import java.util.InputMismatchException; //导入依赖的package包/类
/**
* Validate otp if chosen mode is mobile
*/
@Override
public void validateOtpMMode(String userId, String otp) throws ValidationException {
MemcacheService cache = AppFactory.get().getMemcacheService();
if (cache.get("OTP_" + userId) == null) {
xLogger.warn("OTP expired or already used to generate new password for " + userId);
throw new InputMismatchException("OTP not valid");
}
if (otp.equals(cache.get("OTP_" + userId))) {
cache.delete("OTP_" + userId);
} else {
xLogger.warn("Wrong OTP entered for " + userId);
throw new ValidationException("UA002",userId);
}
}
示例7: validateInputs
import java.util.InputMismatchException; //导入依赖的package包/类
private void validateInputs() throws InputMismatchException {
String title = titleTextField.getText();
if (title.isEmpty()) {
throw new InputMismatchException("Please specify a title.");
}
try {
int width = Integer.parseInt(widthTextField.getText());
int height = Integer.parseInt(heightTextField.getText());
if (width < 1 || width > 10000) {
throw new NumberFormatException();
}
if (height < 1 || height > 10000) {
throw new NumberFormatException();
}
} catch (NumberFormatException exception) {
throw new InputMismatchException("The values for width and height must be integer numbers between 1 and 10000.");
}
}
示例8: exportBitmap
import java.util.InputMismatchException; //导入依赖的package包/类
private void exportBitmap() {
try {
validateInputs();
int originalWidth = document.getWidth();
int width = Integer.parseInt(widthTextField.getText());
double scaleFactor = (double) width / (double) originalWidth;
if (mainController.getGUIController().exportBitmap((Stage) this.getWindow(), scaleFactor)) {
close();
}
} catch (InputMismatchException exception) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.initOwner(getWindow());
alert.setContentText(exception.getMessage());
alert.showAndWait();
}
}
示例9: validateInputs
import java.util.InputMismatchException; //导入依赖的package包/类
private void validateInputs() throws InputMismatchException {
try {
int width = Integer.parseInt(widthTextField.getText());
int height = Integer.parseInt(heightTextField.getText());
if (width < 1 || width > 10000) {
throw new NumberFormatException();
}
if (height < 1 || height > 10000) {
throw new NumberFormatException();
}
} catch (NumberFormatException exception) {
throw new InputMismatchException("The values for width and height must be integer numbers between 1 and 10000.");
}
}
示例10: run
import java.util.InputMismatchException; //导入依赖的package包/类
public void run() {
sc = new Scanner(System.in);
clear();
drawLifeInvader();
drawSeparator();
show("Hello young entrepreneur, today you're gonna change the world !");
show("What's the name of your socialNetwork ?");
show("Your choice : ");
String input = "";
do {
try {
input = sc.nextLine();
} catch (InputMismatchException e) {
show("Please select a correct option");
sc.next();
}
} while (input.equals(""));
socialNetworkName = input;
controller.setUpSocialNetwork(input);
isServer();
}
示例11: runCalibration
import java.util.InputMismatchException; //导入依赖的package包/类
/**
* Run system calibration
*
* @throws IOException
* @throws InputMismatchException
*/
public long runCalibration() throws InputMismatchException, IOException {
final int ITERATIONS = 10;
long total = 0;
for (int i = 0; i < ITERATIONS; i++) {
long calBegin = System.currentTimeMillis();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
gen.generateText(PARAGRAPH, LINE_WIDTH, TOTAL_CHARS, baos);
baos.close();
long calEnd = System.currentTimeMillis();
total = calEnd - calBegin;
}
log.debug("Calibration: {} ms", total / ITERATIONS);
return total / ITERATIONS;
}
示例12: convertInt
import java.util.InputMismatchException; //导入依赖的package包/类
public static int convertInt(byte[] bytes) {
if (bytes.length != NekoIOConstants.INT_LENGTH) {
throw new InputMismatchException(
"Number of bytes provided does not match NekoIOConstants.INT_LENGTH");
}
int val = 0;
for (int i = 0; i < NekoIOConstants.INT_LENGTH; i++) {
val <<= 8;
if (NekoIOConstants.BIG_ENDIAN) {
val |= Byte.toUnsignedInt(bytes[i]);
} else {
val |= Byte.toUnsignedInt(bytes[NekoIOConstants.INT_LENGTH - 1 - i]);
}
}
log.log(Level.FINEST, String.format("%d", val));
return val;
}
示例13: next
import java.util.InputMismatchException; //导入依赖的package包/类
private int next() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
示例14: nextInt
import java.util.InputMismatchException; //导入依赖的package包/类
public int nextInt() {
int c = skipWhileSpace();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = next();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c-'0';
c = next();
} while (!isSpaceChar(c));
return res*sgn;
}
示例15: nextLong
import java.util.InputMismatchException; //导入依赖的package包/类
public long nextLong() {
int c = skipWhileSpace();
long sgn = 1;
if (c == '-') {
sgn = -1;
c = next();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c-'0';
c = next();
} while (!isSpaceChar(c));
return res*sgn;
}