Java 计算字符串中的特定字符
我对编码是新手。如果有人可以帮助我:在这段代码中寻找解决方案,如何计算在随机生成的字符串中可以找到“END”的次数 谢谢你们Java 计算字符串中的特定字符,java,string,search,Java,String,Search,我对编码是新手。如果有人可以帮助我:在这段代码中寻找解决方案,如何计算在随机生成的字符串中可以找到“END”的次数 谢谢你们 public class Tombos { public static void main (String[] args) throws FileNotFoundException { PrintStream out1 = System.out; PrintStream out2 = new PrintStream(new File("out.tx
public class Tombos {
public static void main (String[] args) throws FileNotFoundException {
PrintStream out1 = System.out;
PrintStream out2 = new PrintStream(new File("out.txt"));
String betuk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int i;
for (i=0; i<1000; i++) {
char random = betuk.charAt((int)(26*Math.random()));
out2.print(random);
}
Scanner beolvas = new Scanner(new File("out.txt"));
String x = beolvas.next();
if (x.contains("END")) {
out1.print( "tartalmaz"); // include "END"
} else {
out1.print( "nem tartlmaz"); // not include "END"
}
}
}
公共类Tombos{
公共静态void main(字符串[]args)引发FileNotFoundException{
PrintStream out1=System.out;
PrintStream out2=新的PrintStream(新文件(“out.txt”);
字符串betuk=“abcdefghijklmnopqrstuvxyz”;
int i;
对于(i=0;i您可以使用StringUtils
类的countMatches
方法
int count = StringUtils.countMatches(x, "END");
它计算一个字符串在另一个字符串中出现的次数。只需创建一个新的int变量,如int count=0;
if (x.contains("END)) {
count++;
}
每次发现变量的“结束”增量,然后只打印变量。下面是我要做的:
首先,创建一个新的int变量。我们称它为count
。
int count;
然后,在for循环的主体中,每次找到匹配的字符串时,将一个添加到count
,如下所示:count=count+1
该程序应如下所示:
int i;
int count;
for (i=0;i<1000;i++) {
char random=betuk.charAt((int)(26*Math.random()));
out2.print(random);
}
Scanner beolvas=new Scanner(new File("out.txt"));
String x=beolvas.next();
if (x.contains("END")) {
out1.print( "tartalmaz"); // include "END"
count = count + 1; // add one to the count of matches
} else {
out1.print( "nem tartlmaz"); // not include "END"
}
inti;
整数计数;
对于(i=0;i我不确定您使用随机字符串和文件扫描程序的确切目的是什么,但执行您询问的字符串计数的基本方法是使用正则表达式(the):
请注意,如果您在循环中的某个地方执行此操作,或者反复调用该方法,那么最终将使Pattern.compile()更高效
调用发生在只调用一次的地方,生成的模式存储起来以供重用。如果您是Apache Commons库的用户,StringUtils#countMatches
将完成这项工作
int count = StringUtils.countMatches(x, "END");
如果不是,我可能会使用String#split
int count = x.split("END").length - 1;
这有点难看,但很简单,而且是一行:)这里有一个算法方法:
String x = "ENDDOFJUESADNFENDEJFDNFDENDD"; // arbitrary String
boolean moreMatches = true;
int index = 0, count = 0;
while(moreMatches) {
if (x.indexOf("END", index) > -1)
{
index = x.indexOf("END", index) + 1;
count++;
} else {
moreMatches = false;
}
}
System.out.print(count); // prints 3
请阅读文档以了解我所做的工作
每次我在字符串中找到单词“END”
,我都会再次搜索,但这次搜索的是“END”
从上次找到它后的位置开始。当我再也找不到它时,我就完成了循环。当前的问题到底是什么?好吧,代码就是这样工作的。我只是找不到任何解决方案来计算在那里可以找到多少该死的端点。在尝试读取它之前,你可能想刷新或关闭out2
。更好的是,您可以将随机的char
s累积到StringBuilder
上,然后在上面计算的“END”
s,而不涉及文件系统。countMatches
非常酷,可能是“正确”的方法-我只是想他应该看看如何“手动”完成它因为他是编程新手。@HawkWeisman说得很对;编写这种方便的方法一次就可以成为一种学习经验。当然,在那之后,人们更愿意使用已经实现的版本。@DennisMeng:哦,当然!我只是认为对于初学者来说,用“艰难的方式”来做这件事很重要第一次是为了让他们理解库方法在做什么。这只会计算每个字符串出现一次“END”(在本例中,只有一个字符串可以对其进行计算-文件中没有换行符).啊,你说得对-我没有仔细阅读他的代码。我将在if
语句中添加一个while
循环,以计数“END”
的每一次出现。我首先尝试了这种方法,然后我注意到如上所述,它被计数了一次。我也尝试了这种方法,StringUtils不起作用(由于缺少apache commons库?)字符串#splite工作得很好!谢谢帮助!是的,您需要在类路径上安装apache commons jar才能使StringUtils工作。这将无法编译,也无法工作…增加您的变量
?这意味着什么?
String x = "ENDDOFJUESADNFENDEJFDNFDENDD"; // arbitrary String
boolean moreMatches = true;
int index = 0, count = 0;
while(moreMatches) {
if (x.indexOf("END", index) > -1)
{
index = x.indexOf("END", index) + 1;
count++;
} else {
moreMatches = false;
}
}
System.out.print(count); // prints 3