如果字符串不等于空且大于等于 10 且小于 1000,如何进行连接?



我有一个应用程序,其中包含用户输入金额的编辑文本,如果金额不等于 null 并且必须大于等于 10 且小于等于 1000,我必须制定条件。但是我得到了错误。

法典:-

if (!hasFocus) {
if (m_szAmount.length()==0 && Integer.parseInt(m_szAmount) < 10 && Integer.parseInt(m_szAmount) > 1000) {
m_InputPointsLayout.setErrorEnabled(true);
m_InputPointsLayout.setError(getResources().getString(R.string.enter_points_error));
} else {
m_InputPointsLayout.setErrorEnabled(false);
m_InputPointsLayout.setError(null);
}
}

由以下原因引起:java.lang.NumberFormatException: 无效的整数:">

更改你的 if 语句。您的问题说您要检查条件 1、2 和 3,但您已经完成了OR(||(

if ((m_szAmount.length()!=0 && Integer.parseInt(m_szAmount)>=10 && Integer.parseInt(m_szAmount)<=1000) && (m_operatorSpinner.getSelectedItemPosition() > 0 && m_circleSpinner.getSelectedItemPosition() > 0)) {
m_SubmitButton.setEnabled(true);
}

检查m_szAmount是否仅包含数字,如果m_szAmount包含任何字符串,那么它将抛出数字格式异常。

m_szAmount = m_InputPoints.getText((.toString((.trim((;

if (RewardUtil.isConnected(mContext)) {
if (!TextUtils.isEmpty(m_szAmount)) {
if (m_szAmount.matches("[0-9]+")) {
int amount = Integer.parseInt(m_szAmount);
if (amount >= 10 && amount <= 1000) {
m_SubmitButton.setEnabled(true);
} else {
m_SubmitButton.setEnabled(true);

}
} else {
Log.d("format_issue", "number_format_issue");
}
}
} else {
try {
CSnackBar.showSnackBarError(m_MainLayout, getString(R.string.no_internet_connection_warning));
} catch (Exception e) {
e.printStackTrace();
}
m_SubmitButton.setEnabled(false);
}

请检查下面添加的功能。 首先,您必须检查天气字符串是否为空。 如果它不为空,则检查它是否为空。 如果它不为空,则检查其中的值是否为整数。

private int checkIntegerValue(String strComparedValue)
{
int xAmount = 0;
if(strComparedValue != null && !strComparedValue.isEmpty())
{
try
{
xAmount = Integer.parseInt(strComparedValue);
}
catch(Exception ex)
{
// Your exception if string contains non integer value
}
}
return xAmount;
}

您的代码

if (!hasFocus) {
if (checkIntegerValue(m_szAmount) < 10 && checkIntegerValue(m_szAmount) > 1000) {
m_InputPointsLayout.setErrorEnabled(true);
m_InputPointsLayout.setError(getResources().getString(R.string.enter_points_error));
} else {
m_InputPointsLayout.setErrorEnabled(false);
m_InputPointsLayout.setError(null);
}
}

NumberFormatException 是一个异常,当您 尝试将字符串转换为数字,其中该数字可能是一个 国际 .

首先检查isEmpty()和使用&&运算符而不是||

纠正您的if声明

m_szAmount = m_InputPoints.getText().toString().trim();
if(!m_szAmount.isEmpty())
{
if (RewardUtil.isConnected(mContext)) 
{
if (Integer.parseInt(m_szAmount)>=10 && Integer.parseInt(m_szAmount)<=1000) && (m_operatorSpinner.getSelectedItemPosition() > 0 && m_circleSpinner.getSelectedItemPosition() > 0)) {
m_SubmitButton.setEnabled(true);
} else {
m_SubmitButton.setEnabled(false);
}
} 
else 
{
try {
CSnackBar.showSnackBarError(m_MainLayout, getString(R.string.no_internet_connection_warning));
} catch (Exception e) {
e.printStackTrace();
}
m_SubmitButton.setEnabled(false);
}
}
else
{
Toast.makeText(YourActivityName.this,"String is Empty",Toast.LENGTH_SHORT).show();
}

相关内容

最新更新