문제

So essentially I have an object of vectors and in a specific instance called 'comments' I have comments that actually have String in them and some that are just white space or have a single space. I tried to separate these from each other and I am running into trouble. Everything is outputted for some strange reason. Any thoughts? (display is a JTextArea)

     for (int x = 0; x<dogParkProgramMain.infoVector.size(); x++)    
     {

        if(dogParkProgramMain.infoVector.elementAt(x).getComment() == "//s" || dogParkProgramMain.infoVector.elementAt(x).getComment() == null){
            System.out.println("Blank");

        }
        else{                           
            display.append("Name: " + dogParkProgramMain.infoVector.elementAt(x).getFName() + " " + dogParkProgramMain.infoVector.elementAt(x).getLName() + newLine);
            display.append("Comment: " + dogParkProgramMain.infoVector.elementAt(x).getComment() + newLine);
            display.append("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------" + newLine);
        }
    } // end first for loop
도움이 되었습니까?

해결책

Not sure what you mean by \\s it's since you're not using regular expressions there. The following check should work:

String comment = dogParkProgramMain.infoVector.elementAt(x).getComment();

if (comment == null || comment.trim().isEmpty()) {
  System.out.println("Blank");
}

trim() function will remove all leading and trailing spaces from the string meaning that if your string only contains spaces it would end up empty after trim. This code will check for any number of spaces as opposed to only one space mentioned in your question.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top