英文:
Deleting Last Character From a String in Java
问题
private void passFieldKeyTyped(java.awt.event.KeyEvent evt) {
String pw1 = new String(passField.getPassword());
if (passField.getPassword().length == 10) {
try {
StringBuffer bf = new StringBuffer(pw1);
bf.deleteCharAt(10);
String pw2 = new String(bf);
passField.setText(pw2);
JOptionPane.showMessageDialog(this, "<html><h4>Password Must Not Contain More Than 10 Characters !</h4></html>", "Error !", JOptionPane.ERROR_MESSAGE);
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
}
I'm still a newbie for programming. I hope someone can help me with this. Thanks!
英文:
I'm writing a java code which gives an error when typing more than 10 characters in a PasswordField and cannot type from there on. I tried deleting the last character from the PasswordField on a KeyPressed event but instead of deleting the last character, it deletes the character before it and replace it with the last character.Here goes my code.
private void passFieldKeyTyped(java.awt.event.KeyEvent evt) {
String pw1 = new String(passField.getPassword());
if(passField.getPassword().length==10){
try{
StringBuffer bf = new StringBuffer(pw1);
bf.deleteCharAt(10);
String pw2 = new String(bf);
passField.setText(pw2);
JOptionPane.showMessageDialog(this, "<html><h4>Password Must Not Contain More Than 10 Characters !</h4></html>", "Error !", JOptionPane.ERROR_MESSAGE);
}
catch(Exception e){
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
I'm still a newbie for programming. I hope someone can help me with this. Thanks !
答案1
得分: 0
public class RemoveChar {
public static void main(String[] args) {
String str = "India is my country";
System.out.println(charRemoveAt(str));
}
public static String charRemoveAt(String str) {
return str.substring(0, str.length() - 1);
}
}
英文:
public class RemoveChar {
public static void main(String[] args) {
String str = "India is my country";
System.out.println(charRemoveAt(str,));
}
public static String charRemoveAt(String str) {
return str.substring(0, str.length()-1);
}
}
答案2
得分: 0
我猜你想从字符串中删除一个字符。
你可以在这里使用 StringBuilder 类来删除指定位置的字符。首先将你的字符串转换为 StringBuilder -
StringBuilder sb = new StringBuilder(inputString);
然后你可以使用内置的方法 deleteCharAt()
来删除你想要位置上的字符,像这样 -
sb.deleteCharAt(10);
希望这能帮到你。
英文:
I guess you are trying to delete a char from the String.
You can use the StringBuilder class here to delete a character at your specified position. First convert your String to StringBuilder -
StringBuilder sb = new StringBuilder(inputString);
Then you can use the built in method deleteCharAt()
to delete the char at your desired position like this -
sb.deleteCharAt(10);
Hope this may help you.
答案3
得分: 0
抱歉,我只能返回翻译好的部分,不提供问题回答。
Guys,非常抱歉,我发现这是我的一个错误。实际上我需要再次检查密码字段的长度,长度必须为11,以便删除第10个字符。以下是代码。它有效了。谢谢大家的帮助!!!!!!
private void passFieldKeyTyped(java.awt.event.KeyEvent evt) {
String pw1 = new String(passField.getPassword());
if(passField.getPassword().length==11){
try{
StringBuffer bf = new StringBuffer(pw1);
bf.deleteCharAt(10);
String pw2 = new String(bf);
passField.setText(pw2);
JOptionPane.showMessageDialog(this, "<html><h4>Password Must Not Contain More Than 10 Characters !</h4></html>", "Error !", JOptionPane.ERROR_MESSAGE);
}
catch(Exception e){
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
}
英文:
Guys I'm so sorry I found out it was a mistake of mine..I actually had to check the length of the passwordfield again and length must be 11 to delete the 10th character. Here goes the code.It worked..Thanks for helping guys !!!!!!!!
private void passFieldKeyTyped(java.awt.event.KeyEvent evt) {
String pw1 = new String(passField.getPassword());
if(passField.getPassword().length==11){
try{
StringBuffer bf = new StringBuffer(pw1);
bf.deleteCharAt(10);
String pw2 = new String(bf);
passField.setText(pw2);
JOptionPane.showMessageDialog(this, "<html><h4>Password Must Not Contain More Than 10 Characters !</h4></html>", "Error !", JOptionPane.ERROR_MESSAGE);
}
catch(Exception e){
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论