package Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
public class Stringchar {
public static void main(String[] args) {
int count =0;
String s = "mmamma";
//System.out.println(s.length());
LinkedHashSet<Character> ch = new LinkedHashSet<Character>();
for (int i=0; i<s.length(); i++){
ch.add(s.charAt(i));
}
Iterator<Character> iterator = ch.iterator();
while(iterator.hasNext()){
Character st = (Character) iterator.next();
for (int k=0; k<s.length() ; k++){
if(charAt(k)== st){ // Why this charAt method is not working?
count = count+1;
}
if(count>1) {
System.out.println("Occurance of "+ st + "is" + count);
}
}
}
}
}
I am new to coding so I might be silly in asking this question. I have written a code where I am trying to print the occurrences and the number of the same of one character in a string using sets however I am facing some issues in doing so. Request you to help.
我是编码的新手,所以我可能在问这个问题时很傻。我编写了一个代码,我试图使用集合打印字符串中一个字符的出现次数和相同数量,但是我在这方面遇到了一些问题。请求你帮忙。
6 个解决方案
#1
1
Here:
charAt(k);
is basically the same as
基本上是一样的
this.charAt(k);
In other words: you are trying to invoke a method on the class this code sits in.
换句话说:您正在尝试在此代码所在的类上调用方法。
I assume you intended to do someStringVariable.charAt(k)
instead! ( sure, you meant s.charAt()
, but s
is a terrible, nothing telling name for a variable. Your variables are your pets, give them names that mean something!)
我假设你打算做someStringVariable.charAt(k)! (当然,你的意思是s.charAt(),但是对于变量来说,s是一个可怕的,没有说出名字。你的变量是你的宠物,给它们起名字意味着什么!)
#2
0
The method charAt
is not static and need to be applied on a given String
, if not how to know where to look for the xxth char ?
方法charAt不是静态的,需要应用于给定的String,如果不知道如何知道在哪里查找xxth char?
str.charAt(index);
Also, the print
operation would better be after the for loop
which counts the occurences, if not you'll have a print at each occurence
此外,打印操作最好是在for循环之后计算出现次数,如果没有,你会在每次出现时都有打印
for (int k=0; k<s.length() ; k++){
if(s.charAt(k) == st){
count = count+1;
}
}
if(count>1) {
System.out.println("Occurance of "+ st + "is" + count);
}
#3
0
I suppose you want to check, how often the Character appears in your string (String s = "mmamma";
).
我想你要检查字符在字符串中出现的频率(String s =“mmamma”;)。
The charAt()
method has to be applied on a String object, so you have to change the if condition from this:
charAt()方法必须应用于String对象,因此您必须更改if条件:
if(charAt(k) == st)
To this:
if(s.charAt(k) == st)
#4
0
The problem is that you are trying to get a character at a position of a character. When you create the variable st
it is a character and will have a length of 1; there fore you are unable to get a charAt(index)
there. Additionally this method of using the LinkedHashSet
will not work because when you add those characters to the LinkedHashSet
it will not add each character more than once. Instead you want an ArrayList
.
问题是你试图在一个角色的位置上获得一个角色。当你创建变量st时,它是一个字符,长度为1;因此,你无法在那里得到一个charAt(索引)。此外,这种使用LinkedHashSet的方法不起作用,因为当您将这些字符添加到LinkedHashSet时,它不会多次添加每个字符。相反,你想要一个ArrayList。
This is probably not the most efficient solution but it will accomplish what you are trying to do with the HashSet
这可能不是最有效的解决方案,但它将完成您尝试使用HashSet做的事情
String s = "mmamma";
List<Character> characterList = new ArrayList<>();
LinkedHashSet<Character> characterLinkedHashSet = new LinkedHashSet<>();
for(char c : s.toCharArray()) {
characterLinkedHashSet.add(c);
characterList.add(c);
}
for (Character character : characterLinkedHashSet) {
int frequency = Collections.frequency(characterList, character);
System.out.println("The frequency of char " + character + " is " + frequency);
}
So what this does it is creates your LinkedHashSet
as well as an ArrayList
. The ArrayList
stores all of the characters in a Collection
and the LinkedHashSet
stores only one instance of each Character. We can then loop over the HashSet
and get the frequency inside the ArrayList
这就是它创建了LinkedHashSet和ArrayList。 ArrayList存储Collection中的所有字符,LinkedHashSet仅存储每个Character的一个实例。然后我们可以遍历HashSet并获取ArrayList中的频率
#5
0
Thanks all for your replies. I just thought that I will be answered in days (if at all). But this is amazing.. so much help and so quick. You guys are awesome. Thanks much!
谢谢大家的回复。我只是觉得我会在几天内回答(如果有的话)。但这太棒了......太多的帮助和快速。你们真棒。非常感谢!
#6
0
You have to correct your code like so,
你必须像这样纠正你的代码,
while (iterator.hasNext()) {
int count = 0;
Character st = (Character) iterator.next();
for (int k = 0; k < s.length(); k++) {
if (s.charAt(k) == st) { // Why this charAt method is not working?
count++;
}
}
if (count > 1) {
System.out.println("Occurance of " + st + " is: " + count);
}
}
charAt
method is available in String
class hence you have to call it on a String
reference. I have made few more improvements to the code too. Declare the count variable inside the while
loop which is less error prone. Finally notice that I have moved the if
statement away from the for
loop since it gives some spurious intermediary results if it is kept inside the for
loop.
charAt方法在String类中可用,因此您必须在String引用上调用它。我对代码也做了很多改进。在while循环中声明count变量,这更容易出错。最后请注意,我已将if语句从for循环中移开,因为如果它保留在for循环内,它会产生一些虚假的中间结果。
#1
1
Here:
charAt(k);
is basically the same as
基本上是一样的
this.charAt(k);
In other words: you are trying to invoke a method on the class this code sits in.
换句话说:您正在尝试在此代码所在的类上调用方法。
I assume you intended to do someStringVariable.charAt(k)
instead! ( sure, you meant s.charAt()
, but s
is a terrible, nothing telling name for a variable. Your variables are your pets, give them names that mean something!)
我假设你打算做someStringVariable.charAt(k)! (当然,你的意思是s.charAt(),但是对于变量来说,s是一个可怕的,没有说出名字。你的变量是你的宠物,给它们起名字意味着什么!)
#2
0
The method charAt
is not static and need to be applied on a given String
, if not how to know where to look for the xxth char ?
方法charAt不是静态的,需要应用于给定的String,如果不知道如何知道在哪里查找xxth char?
str.charAt(index);
Also, the print
operation would better be after the for loop
which counts the occurences, if not you'll have a print at each occurence
此外,打印操作最好是在for循环之后计算出现次数,如果没有,你会在每次出现时都有打印
for (int k=0; k<s.length() ; k++){
if(s.charAt(k) == st){
count = count+1;
}
}
if(count>1) {
System.out.println("Occurance of "+ st + "is" + count);
}
#3
0
I suppose you want to check, how often the Character appears in your string (String s = "mmamma";
).
我想你要检查字符在字符串中出现的频率(String s =“mmamma”;)。
The charAt()
method has to be applied on a String object, so you have to change the if condition from this:
charAt()方法必须应用于String对象,因此您必须更改if条件:
if(charAt(k) == st)
To this:
if(s.charAt(k) == st)
#4
0
The problem is that you are trying to get a character at a position of a character. When you create the variable st
it is a character and will have a length of 1; there fore you are unable to get a charAt(index)
there. Additionally this method of using the LinkedHashSet
will not work because when you add those characters to the LinkedHashSet
it will not add each character more than once. Instead you want an ArrayList
.
问题是你试图在一个角色的位置上获得一个角色。当你创建变量st时,它是一个字符,长度为1;因此,你无法在那里得到一个charAt(索引)。此外,这种使用LinkedHashSet的方法不起作用,因为当您将这些字符添加到LinkedHashSet时,它不会多次添加每个字符。相反,你想要一个ArrayList。
This is probably not the most efficient solution but it will accomplish what you are trying to do with the HashSet
这可能不是最有效的解决方案,但它将完成您尝试使用HashSet做的事情
String s = "mmamma";
List<Character> characterList = new ArrayList<>();
LinkedHashSet<Character> characterLinkedHashSet = new LinkedHashSet<>();
for(char c : s.toCharArray()) {
characterLinkedHashSet.add(c);
characterList.add(c);
}
for (Character character : characterLinkedHashSet) {
int frequency = Collections.frequency(characterList, character);
System.out.println("The frequency of char " + character + " is " + frequency);
}
So what this does it is creates your LinkedHashSet
as well as an ArrayList
. The ArrayList
stores all of the characters in a Collection
and the LinkedHashSet
stores only one instance of each Character. We can then loop over the HashSet
and get the frequency inside the ArrayList
这就是它创建了LinkedHashSet和ArrayList。 ArrayList存储Collection中的所有字符,LinkedHashSet仅存储每个Character的一个实例。然后我们可以遍历HashSet并获取ArrayList中的频率
#5
0
Thanks all for your replies. I just thought that I will be answered in days (if at all). But this is amazing.. so much help and so quick. You guys are awesome. Thanks much!
谢谢大家的回复。我只是觉得我会在几天内回答(如果有的话)。但这太棒了......太多的帮助和快速。你们真棒。非常感谢!
#6
0
You have to correct your code like so,
你必须像这样纠正你的代码,
while (iterator.hasNext()) {
int count = 0;
Character st = (Character) iterator.next();
for (int k = 0; k < s.length(); k++) {
if (s.charAt(k) == st) { // Why this charAt method is not working?
count++;
}
}
if (count > 1) {
System.out.println("Occurance of " + st + " is: " + count);
}
}
charAt
method is available in String
class hence you have to call it on a String
reference. I have made few more improvements to the code too. Declare the count variable inside the while
loop which is less error prone. Finally notice that I have moved the if
statement away from the for
loop since it gives some spurious intermediary results if it is kept inside the for
loop.
charAt方法在String类中可用,因此您必须在String引用上调用它。我对代码也做了很多改进。在while循环中声明count变量,这更容易出错。最后请注意,我已将if语句从for循环中移开,因为如果它保留在for循环内,它会产生一些虚假的中间结果。