我开始这是因为我完全无聊但是由于这个错误,我一直坐在这里很久以来最后决定把它带到stackOverFlow。这是我写的代码。
我试图通过跳过1个索引来打印字符。但是当有重复时,我想打印一个空格,这会使大字符串中的单词不同。
更新的问题:一切都已修复,除了我不能增加I值超过1.我在下面的程序中评论它。请看一下。
让我切断追逐并达到目的。我需要这个输出" Vishnu Vardhan"来自这个字符串" aVeIwSjHaNgUaaVdAgRjDkHxAmN&#34 ;;
我唯一的要求是,如果字符串有两个相同的字母,则必须打印空格。所以" aVeIwSjHaNgU [aa] VdAgRjDkHxAmN"括号中的aa必须用空格代替。
它必须是动态的,如果重复任何字符,它必须打印一个空格并跳转到所需的下一个字符并打印它。
这是更新的程序。使用其中一条评论的帮助。
公共阶层Decrypter {
public static void main(String[] args) {
String Text = "aVeIwSjHaNgUaaVdAgRjDkHxAmN";
char[] convertedText = Text.toCharArray();//converted string to char array
for (int i = 1; i < convertedText.length; i++) { //Looping it to print alternate chars
/* if the character at an index is same as the character at next index then
add a space and increase index value by 2 so I can print the required char*/
if (i + 1 < convertedText.length) {
if (Text.charAt(i) == Text.charAt(i + 1)) {
i++;// Increasing I value by 2 here will give me required output. Everything is perfect now
System.out.printf("%s ", convertedText[i]);
} else {
System.out.printf("%s", convertedText[i]);
i++;
}
}
}
}
}
当前输出:VISHNUadgjkxm
必需的输出:VISHNU VARDHAN
答案 0 :(得分:0)
我不知道是否需要将字符串转换为charArray,但我希望这样做。如果您有问题需要修改,请在下方发表评论。
String text = "aVeIwSjjHaNgUkkVarqddlhxn";
//this is the holder of your new processed text.
String newText = "";
//start of counter. it may start in any number depends on requirements.
int x = 0;
//loop while the x is lessthan the length of your string.
while(x < text.length()){
//check if the x + 1 is not equal to the length of your string to avoid StringIndexOutOfBoundsException
if((x+1) != text.length()){
//in this area it will check if the current char is the same on next index
if(text.charAt(x) == text.charAt(x+1)){
// this will concatenate/append the value of char with space
newText += text.charAt(x) +" ";
// this will increase the value of your x by 1 and at the bottom there are also x++ that will add 1 to your x so the total sum of x if (text.charAt(x) == text.charAt(x+1)) are true, will be 2.
x++;
}
}
newText += text.charAt(x);
x++;
}
System.out.println(newText);
输出:
aVeIwSj jHaNgUk kVarqd dlhxn
如果这不是您要找的,请更新您的问题。
答案 1 :(得分:0)
修正:
/**
*
* @author Chintu
*/
public class Decrypter {
public static void main(String[] args) {
String Text = "aVeIwSjHaNgUkkVarqdlhxn";
char[] convertedText = Text.toCharArray();//converted string to char array
for (int i = 1; i < convertedText.length; i++) { //Looping it to print alternate chars
/* if the character at an index is same as the character at next index then
add a space and increase index value by 2 so I can print the required char*/
if (i+1 < convertedText.length) {
if (Text.charAt(i) == Text.charAt(i + 1)) {
i++;
System.out.printf("%s ",convertedText[i]);
}
}
System.out.printf("%s", convertedText[i]);
}
}
}
答案 2 :(得分:0)
固定
公共阶层Decrypter {
public static void main(String[] args) {
String Text = "aVeIwSjHaNgUaaVdAgRjDkHxAmN";
char[] convertedText = Text.toCharArray();//converted string to char array
for (int i = 0; i < convertedText.length; i++) { //Looping it to print alternate chars
/* if the character at an index is same as the character at next index then
add a space and increase index value by 2 so I can print the required char*/
if (i + 1 < convertedText.length) {
if (Text.charAt(i) == Text.charAt(i + 1)) {
i += 2;
System.out.printf(" %s", convertedText[i]);
} else {
i++;
System.out.printf("%s", convertedText[i]);
}
}
}
}
}
输出:VISHNU VARDHAN