+ 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 🤔
2 Réponses
+ 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.");
}
}
}
+ 1
Thank u mate, it was a huge help 😊👍