Java 我想删除动词的最后两个字母,并使用StringBuilder对其进行共轭

Java 我想删除动词的最后两个字母,并使用StringBuilder对其进行共轭,java,stringbuilder,Java,Stringbuilder,评论中解释了一切 Scanner sc=new Scanner(System.in); //asking for the verb System.out.println("Donnez un verbe regulier du premier groupe :"); //putting the verb in the string String chaine=sc.nextLine(); //taking the two last elements char first=chaine.ch

评论中解释了一切

Scanner sc=new Scanner(System.in);

//asking for the verb
System.out.println("Donnez un verbe regulier du premier groupe :");

//putting the verb in the string
String chaine=sc.nextLine();

//taking the two last elements
char first=chaine.charAt(chaine.length());
char second=chaine.charAt(chaine.length()-1);

//creating a StringBuilder:
StringBuilder sb=new StringBuilder();

//putting string in the builder :
sb.append(chaine);

//deleting the two last characters :
sb.deleteCharAt(sb.length()-1);


//printing elemnts :
System.out.println("Je "+sb.append("e"));
System.out.println("Tu "+sb.append("es"));
System.out.println("Il/Elle "+sb.append("e"));
System.out.println("nous "+sb.append("ons"));
System.out.println("vous "+sb.append("ez"));
System.out.println("Ils/Elles "+sb.append("ent"));
我有StringIndexOutOfBoundsException

我可以根据您的问题提供更多帮助

String radical = chaine.replaceFirst("..$", "");

System.out.println("Je " + radical + "e");
System.out.println("Tu " + radical + "es");
System.out.println("Il/Elle " + radical + "e");
System.out.println("Nous " + radical + "ons");
System.out.println("Vous " + radical + "ez");
System.out.println("Ils/Elles " + radical + "ent");
这首先使用正则表达式。模式:

. 任何字符 . 任何字符 $字符串结尾 因此:最后两个字母将替换为空字符串

与chaine.substring0、chaine.length-2相比的次要优势 对于空字符串或单字母字符串,不会发生索引错误;
它不需要更换。诚然,子字符串更快。

您的代码充满了错误。 第一:

将引发异常,因为chaine的最后一个字符是chaine.length-1。所以你应该写:

char first=chaine.charAt(chaine.length()-1);
char second=chaine.charAt(chaine.length()-2);
或许:

char first=chaine.charAt(chaine.length()-2);
char second=chaine.charAt(chaine.length()-1);
但你似乎对第一个和第二个什么都不做

接下来,StringBuilder.append将把参数附加到生成器中,我不认为这是您不会做的

也许你想做:

String prefix = chaine.substring(0, chaine.length()-2);

System.out.println("Je " + prefix + "e");
System.out.println("Tu " + prefix + "es");
System.out.println("Il/Elle " + prefix + "e");
System.out.println("Nous " + prefix + "ons");
System.out.println("Vous " + prefix + "ez");
System.out.println("Ils/Elles " + prefix + "ent");

首先为什么要使用字符串生成器?您不能只使用子字符串删除最后2个字符,然后进行简单的连接:stringWithout2Chars+esStick到StringBuilder,但也使用子字符串。注释中解释了所有内容,特别是没有注释。请重新格式化。我得到了StringIndexOutOfBoundsException。感谢您提供这些信息,@khmed,它非常有用。提供更多信息时,最好编辑问题并将其添加到那里。这次我是为你做的。我已经撤回了我的投票结果。人们可能还想把这个正则表达式保存到一个变量中:static final Pattern LAST_2_CHARS=Pattern.compile.$;我宁可认为这是一个缺点:如果输入不正确,我们就不希望出错。当然,这首先是一个添加输入验证的问题:if chaine.endsWither…@OleV.V。谢谢,在处理管道中,快速故障确实是很好的。在这里,我宁愿希望chaine.endsWither。。。chaine.substring0、chaine.length-2或replaceFirst的惰性。此外,询问者仅从字符串生成器中删除一个字符:sb.deleteCharAtsb.length-1;。我更喜欢某人的长度。
String prefix = chaine.substring(0, chaine.length()-2);

System.out.println("Je " + prefix + "e");
System.out.println("Tu " + prefix + "es");
System.out.println("Il/Elle " + prefix + "e");
System.out.println("Nous " + prefix + "ons");
System.out.println("Vous " + prefix + "ez");
System.out.println("Ils/Elles " + prefix + "ent");