2017-08-17 63 views
-3

我有一个应用程序,其中包含用户输入金额的编辑文本,如果金额不等于零,并且必须大于等于10且小于等于1000,我必须提出条件。但是我收到错误消息。如果字符串不等于null且大于等于10且小于等于1000,如何制定condtiion?

代码: -

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:无效INT: “”

回答

1

改变你的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); 
    } 
+0

我现在想使 – Niraj

+0

你说的相反 –

+0

我收到错误 – Niraj

0

NumberFormatException的是,可能会被抛出,当你 尝试将字符串转换为一个异常转换为一个数字,其中该数字可能是一个 int。

首先检查一下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(); 
} 
0

请查看如下的添加功能。 首先你要检查天气字符串是否为空。 如果它不是null,那么检查它是否为空。 如果它不是空的,那么检查其中的值是否为整数。

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); 
     } 
    } 
0

检查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); 

        }