Java 调试:如何删除字符串中连续出现的字符?
下面是我删除字符串中连续出现的字符但未得到预期结果的代码Java 调试:如何删除字符串中连续出现的字符?,java,string,algorithm,loops,duplicates,Java,String,Algorithm,Loops,Duplicates,下面是我删除字符串中连续出现的字符但未得到预期结果的代码 import java.util.*; import java.lang.*; import java.io.*; class GFG { public static void main (String[] args) { Scanner sc= new Scanner(System.in); int t=sc.nextInt(); while(t
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void main (String[] args) {
Scanner sc= new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
String name= sc.next();
char[] c = new char[name.length()];
int j=0;
boolean check=true;
//looping through the array to find duplicates
for(int i=0;i<name.length()-1;i++){
if(name.charAt(i)==name.charAt(i+1)){
continue;
}
else{
c[j]=name.charAt(i);
j++;
check=false;
}
}
//printing the char array
if(check==true){
System.out.println(name);
}else{
for(int i=0;i<j+1;i++){
System.out.print(c[i]);
}
System.out.print(name.charAt(name.length()-1));
System.out.println();
}
}
}
}
import java.util.*;
导入java.lang.*;
导入java.io.*;
GFG类{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
int t=sc.nextInt();
而(t-->0){
字符串名称=sc.next();
char[]c=新字符[name.length()];
int j=0;
布尔检查=真;
//在数组中循环查找重复项
对于(int i=0;i您可以使用此方法:
class GFG {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
String name = sc.next();
String result = "";
if (name.length() > 0)
result += name.charAt(0);
//looping through the array to find duplicates
for (int i = 1; i < name.length(); i++){
if (name.charAt(i) == name.charAt(i - 1)){
continue;
} else {
result+=name.charAt(i);
}
}
//printing the result
System.out.print(result);
}
}
GFG类{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
字符串名称=sc.next();
字符串结果=”;
if(name.length()>0)
结果+=名称.字符(0);
//在数组中循环查找重复项
对于(int i=1;i
另一种选择是使用Java8流
步骤:
从index=1
开始一个IntStream
,直到结束。(因为结果值最初等于索引0处的字符)
如果字符与上次看到的字符不匹配,则将其添加到最终结果中
import java.util.*;
导入java.util.stream.IntStream;
GFG类{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
int t=sc.nextInt();
而(t-->0){
字符串名称=sc.next();
字符串结果=IntStream.range(1,name.length())
.mapToObj(i->name.charAt(i)+“”)
.reduce(name.charAt(0)+“”,(p,s)->p.lastIndexOf(s)==p.length()-1?p:p+s);
系统输出打印项次(结果);
}
}
}
注意:它使用简单的字符串连接。好的,您的代码有一些问题
让我们按如下方式标记行号:
01 char[] c = new char[name.length()];
02 int j=0;
03 boolean check=true;
04 //looping through the array to find duplicates
05 for(int i=0;i<name.length()-1;i++){
06 if(name.charAt(i)==name.charAt(i+1)){
07 continue;
08 }
09 else{
10 c[j]=name.charAt(i);
11 j++;
12 check=false;
13 }
14 }
15 //printing the char array
16 if(check==true){
17 System.out.println(name);
18 }else{
19 for(int i=0;i<j+1;i++){
20 System.out.print(c[i]);
21 }
22 System.out.print(name.charAt(name.length()-1));
23 System.out.println();
24 }
01 char[]c=新字符[name.length()];
02 int j=0;
03布尔检查=真;
04//在数组中循环查找重复项
05 for(int i=0;i请使用问题正文提问并简要说明您面临的问题?您能描述一下您的代码面临的问题吗?它是否未编译,返回的结果与预期的不同(对于什么输入以及错误的结果是什么)?我的代码的问题是,在多个测试用例的情况下,它不会生成结果。代码在生成某些测试用例的输出后停止。不要在注释中提供此类信息!请阅读并相应地增强您的问题。提示:变量使用多个字符。这不是数学公式!需要阅读代码对人类来说,这样的缩写毫无意义。
01 StringBuilder sb = new StringBuilder();
02 int lastChar = 65537; // This char will never exist as char is 16 bits in Java.
03 for (int i = 0; i < name.length(); i++) {
04 char c = name.charAt(i);
05 if (c != lastChar) // perform int comparison so first char will be picked up.
06 {
07 sb.append(c);
08 lastChar = c; // widens c into an int.
09 }
10 }
11 System.out.println(sb.toString());