![](/img/trans.png)
[英]Figuring out the character value in finding first non repeated character in a string
[英]Finding a repeated character in a string
该问题指出以下几点:给定字符串和字符,用户查找字符(由用户提供)在字符串中重复自身的次数(也由用户给出)。
我有这段代码
public int repeticion (int s){
int return = 0;
int cont = 0;
Scanner in = new Scanner(System.in);
System.out.println("Write a string: ");
String chain = in.next();
System.out.println("Write the character: ");
String character = in.next();
if (chain.contains(character)) {
cont = cont + 1;
}
System.out.println("The character repeats itself "+cont+"times");
return return;
但是如您所见, .contains
只计算一次字符,而不计算字符出现在字符串中的次数。
.contains()仅表示字符串中存在一个字符,而不是多少。 您需要遍历字符串的每个字符以检查它是否等于要搜索的字符。
String chain = "your string";
int cont = 0;
for(int i=0; i<chain.length(); i++) {
if(chain.charAt(i) == character) {
cont++;
}
}
您还可以反复检查字符是否在字符串中,获取该索引,然后检查从该索引之后到字符串末尾的子字符串。 我建议以下内容:
int index = 0;
int count = 0;
while (chain.indexof(character, index) != -1 && index < chain.length()-1) {
index = chain.indexof(character, index) + 1;
count++;
}
包含将仅告诉您该字符是否存在。 要实际计算该字符出现的次数,您需要遍历字符串并计算所选字符出现的次数。 此方法将起作用:
public countACharacter(char thecharacter, String stringtocountcharactersin) {
int count = 0;
for(int i = 0; i < stringtocountcharactersin.length(); i++) {
if(stringtocountcharactersin.charAt(i) == thecharacter) {
count++;
}
}
return count;
}
//没有字符串方法。 公共静态void main(String [] args){
String ss="rajesh kumar";
Field value = null;
try {
value = String.class.getDeclaredField("value");
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// It's private so we need to explicitly make it accessible.
value.setAccessible(true);
try {
char[] values = (char[])value.get(ss);
//String is converted to char array with out string functions
for (int i = 0; i < values.length; i++) {
int count=0;
for(int j=0;j<values.length;j++)
{
if(values[i]== values[j])
{
count++;
}
}
System.out.println("\n Count of :"+values[i] +"="+count);
}
System.out.println("Values "+values[1]);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.*;
public class Duplicate {
public static void main(String[] args) throws IOException {
int distinct = 0;
int i = 0, j = 0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter STRING : -");
String s = br.readLine();
try {
for (i = 0; i < s.length(); i++) {
while (s != null) {
s = s.trim();
for (j = 0; j < s.length(); j++) {
if (s.charAt(i) == s.charAt(j)) {
distinct++;
}
}
System.out.println(s.charAt(i) + "--" + distinct);
String d = String.valueOf(s.charAt(i));
s = s.replaceAll(d, " ");
distinct = 0;
}
}
} catch (Exception e) {}
}
}
public static void chars( String a, char k)
{
String z=""+k;
int s=0;
for(int i=0;i<a.length();i++)
{
if(z.equals(a.substring(i,i+1)))
s++;
}
System.out.println(s);
}
您可以通过这种方式实现您的目标,而无需任何原因即可使用整数s,而您做错的是使用.contains()方法,而不是将给定的char与字符串的每个char进行比较,然后使用一个用于保持字符次数的计数器,也可以通过将char与一个空string(“”)连接起来,然后将char转换为字符串,然后使用像我在代码中使用过的.equals方法来实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.