我必须从控制台读取菜单选项,选项可以是整数或字符串。我的问题是是否有另一种检查输入的方法是字符串还是整数
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Read {
public Object read(){
String option = null;
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
try {
option = buffer.readLine();
if(isInt(option)){
return Integer.parseInt(option);
} else if(isString(option)){
return option;
}
} catch (IOException e) {
System.out.println("IOException " +e.getMessage());
}
return null;
}
private boolean isString(String input){
int choice = Integer.parseInt(input);
if(choice >= 0){
return false;
}
return true;
}
private boolean isInt(String input){
int choice = Integer.parseInt(input);
if(choice >= 0){
return true;
}
return false;
}
}