當前位置: 首頁>>代碼示例>>Java>>正文


Java InputMismatchException類代碼示例

本文整理匯總了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);
	}
 
開發者ID:a-r-d,項目名稱:java-1-class-demos,代碼行數:19,代碼來源:ReadingIntegers.java

示例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;
}
 
開發者ID:ErikHumphrey,項目名稱:ics4u,代碼行數:27,代碼來源:Interrogator.java

示例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);
	}
}
 
開發者ID:orelshalom,項目名稱:Assignment_1,代碼行數:26,代碼來源:FilteringPlace.java

示例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;
}
 
開發者ID:cjlutterloh,項目名稱:TheaterReservation-University-,代碼行數:19,代碼來源:Main.java

示例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);
	}

}
 
開發者ID:asms,項目名稱:360w17g1,代碼行數:23,代碼來源:AbstractView.java

示例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);
  }
}
 
開發者ID:logistimo,項目名稱:logistimo-web-service,代碼行數:19,代碼來源:AuthenticationServiceImpl.java

示例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.");
    }
}
 
開發者ID:pfolta,項目名稱:Shapify,代碼行數:23,代碼來源:NewDrawingScene.java

示例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();
    }
}
 
開發者ID:pfolta,項目名稱:Shapify,代碼行數:21,代碼來源:ExportBitmapScene.java

示例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.");
    }
}
 
開發者ID:pfolta,項目名稱:Shapify,代碼行數:17,代碼來源:ExportBitmapScene.java

示例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();
}
 
開發者ID:KeydownR,項目名稱:lifeInvader-SchoolProject,代碼行數:22,代碼來源:CliView.java

示例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;
}
 
開發者ID:openkm,項目名稱:document-management-system,代碼行數:23,代碼來源:Benchmark.java

示例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;
}
 
開發者ID:cly753,項目名稱:CX4013-Remote-File-System,代碼行數:18,代碼來源:NekoInputStream.java

示例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++];
}
 
開發者ID:hamadu,項目名稱:competitive-programming-snippets,代碼行數:17,代碼來源:InputReader.java

示例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;
}
 
開發者ID:hamadu,項目名稱:competitive-programming-snippets,代碼行數:19,代碼來源:InputReader.java

示例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;
}
 
開發者ID:hamadu,項目名稱:competitive-programming-snippets,代碼行數:19,代碼來源:InputReader.java


注:本文中的java.util.InputMismatchException類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。