+ 3

How to check if Input is String or Integer by BufferedReader

String input; (new Object of Bufferedreder)= in; input = in.readLine(); if(Integer.parseInt(input)){ . } i get an error: "Cannot convert from int to boolean". so how can I check if the input from user is an Integer or String đŸ€”

15th May 2018, 7:48 PM
Sam
Sam - avatar
2 Answers
+ 5
https://code.sololearn.com/c2i6SoQj7T7g/#java import java.io.*; public class Program { public static void main(String[] args) throws Exception{ String userInput = ""; int inputAsInt = 0; BufferedReader buffer = new BufferedReader(new InputStreamReader (System.in)); System.out.println("Please enter a number: "); try { userInput = buffer.readLine(); inputAsInt = Integer.parseInt(userInput); System.out.println("Input is a number."); } catch (NumberFormatException e) { System.out.println("Input isn't a number."); } } }
15th May 2018, 8:02 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 1
Thank u mate, it was a huge help 😊👍
16th May 2018, 8:40 AM
Sam
Sam - avatar