如果文本框中没有值 return 字符串 Java

If no value in textbox return String Java

我必须比较一个字符串是否在插入的文本框中,如果不是,它应该 return 一个字符串。它不适用于下面的这行代码,有人有想法吗?

if (inString.trim().length() == 0) 
            return "b";

这是整个代码块:

public static String checkYear(String inYear) {


      // Gets the current date and time.
    // Returns the actual year as an int.
    Calendar now = Calendar.getInstance();   // Gets the current date and time.
    int currYear = now.get(Calendar.YEAR);
   try
  {
        int year = Integer.parseInt(inYear);

        if (inYear.trim().length() == 0) 
                 return "b";


            if ((year >= 1900) && (year <= currYear)) 
            return ""; //valid date
            else
            return "Year must be between 1900 and current year";

  }
  catch (Exception ex)
    {
        return "Year must be a Number";
    }

}

我有一个例外。

我用这段代码测试文本块中是否没有任何内容:

    String year = txtYear.getText();
    String msgYear = DVD.checkYear(year); 
    int year2 = 0;

if (msgYear.length() == 1 )
          {
              year2 = 0;

          DVD dvd2 = new DVD(txtTitle.getText(),
                           year2,
                       favouriteCheck);
           dvdCollect.addDVD(dvd2);                
           parent.refreshDVDList();
          } 

假设inString将保存在文本框中输入的字符串,我相信它应该是:

if (inString ==null || (inString!=null && inString.trim().length() ==0))
    return "b" 

代码

if (inYear.trim().length() == 0) 
             return "b";`

行后没有意义

int year = Integer.parseInt(inYear);

如果 inYear 只包含白色 space,Integer.parseInt 将已经抛出 NumberFormatException 并捕获异常。

代码

if (inString == null || inString.trim().isEmpty())
    return "b"; 

应该在 try 块之前。