Read user input until quit is entered

I encountered a computer test question during the interview today, record it:

Read user input until quit is entered

Convert String type to Integer type

When the input is 1, it returns true, and when the input is not 1, it returns false.
Not available. A try and catch catch all exceptions and handle errors when they are empty.

public static void main(String[] args) {
		String l_input;
		Integer a = null;
		BufferedReader bufferedReader = null;
		System.out.println("Please enter a string: ");

		try {
			while (true) {
				bufferedReader = new BufferedReader(new InputStreamReader(System.in));
				l_input = bufferedReader.readLine();

				if(l_input.equals("quit")) {
					System.out.println("finish");
					break;
				}

				// Determine if the string can be converted to an Integer type
				try {
						a = Integer.parseInt(l_input);
					// Determine whether the input is 1, if it is 1, output true, otherwise output false
					if (a == 1) {
						System.out.println(true);
					} else {
						System.out.println(false);
					}
				} catch (NumberFormatException e) {
					System.out.println("Conversion failed: ");
				}

				System.out.println("You entered: " + l_input);
			}

		} catch (IOException e) {
			e.printStackTrace ();
		}

	}
There are two ways to convert String to Integer type:
int a = Integer.parseInte(str);
int a = Integer.valueOf(str).intValue();
Note: Remember to catch exceptions when converting String type to int type,
NumberFormatException



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325797076&siteId=291194637