我必須比較一個字符串是否插入到文本框中,如果沒有,它應該返回一個字符串。它不適用於下面這行代碼,有一個想法?如果文本框中沒有值返回String Java
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();
}
請提供更多的代碼和完整的描述,說明您希望完成什麼,以便您的問題得到充分評估和解決。 – 2015-04-05 23:19:29