0
사용자가 JInputDialog를 통해 입력 한 텍스트 (예 : "식료품 쇼핑으로 이동")를 JList에 추가하는 간단하게 수행하는 목록 프로그램을 코딩했습니다. 이 프로그램은 잘 실행되고 있지만, 나는 다음과 같은 코드를 통해, 텍스트를 입력하거나 공백을 입력하지 않고 대화 상자에서 괜찮 눌러에서 사용자를 방지하기 위해 시도 할 것이라고 생각 :JList에서 빈 항목 제거
//if create button is pressed
}else if(src == create){
//show an input dialog box
String s = JOptionPane.showInputDialog(this, "What do you want to remember?");
/*f the length of the given string is zero, or if the length of the string without spaces
is zero, then tell the user*/
if(s.length() == 0 || removeSpaces(s).length() == 0){
System.out.println("Nothing has been entered");
JOptionPane.showMessageDialog(this, "You must enter a text value!");
//if the string is valid, add it to the file
}else{
sfile.add(s);
System.out.println("Item added to list. " + s.length());
}
}else if(src == close){
System.exit(0);
}
}
//remove all white spaces and tabs from the string
public String removeSpaces(String s){
s.replaceAll("\\s+", "");
return s;
}
}
이 코드 작품과를 보여줍니다 사용자가 아무 것도 입력하지 않았 으면 "아무것도 입력하지 않았습니다."대화 상자가 표시되지만 사용자가 공백을 입력하면 작동하지 않습니다. 내가 도대체 뭘 잘못하고있는 겁니까?
를? –
's.replaceAll ("\\ s +", "");'문자열은 불변이지만 새로운 문자열을 반환하므로's'에 영향을주지 않습니다. – Pshemo