使用 Scanner 在 for 循环中逐字母分析字符串

我需要计算一个字母在字符串中出现的频率。


为此,我考虑使用扫描仪对象,将字符串传递给它


扫描仪 s = 新的扫描仪(字符串)


并使用 next() 方法通过 switch 语句分析每个字符。


我已经在这些板上进行了搜索,并设计了以下内容:-


for (int i = 0; i < outputString.length(); i++) {

    Scanner s = new Scanner(outputString);

    char letter = s.next().charAt(i);

    switch (letter) {

    switch code;

}

当我正在分析的字符串包含除空格之外的任何内容(-_z1 等)时,这似乎有效,这将导致程序抛出 String.IndexOutOfBoundException (4)。


还有什么我可以尝试的,或者我应该从单词中删除所有空格(即我正在考虑通过使用for循环创建一个新单词字符串来添加每个string.charAt(i)!='')。


编辑:我忘记了扫描仪在 for 循环中,我将其取出。其次,不确定它是否会发生变化,但我正在尝试计算字符串中字母表中每个字母出现的次数,而不仅仅是一种类型的字母。感谢您的意见!


提前致谢,


白衣非少年
浏览 215回答 3
3回答

素胚勾勒不出你

我强烈认为您使用这种方法会使事情复杂化。您可以简单地将字符串(以及您正在搜索的字符)传递给一个方法,如下所示:public int checkFreq(char letter, String word){&nbsp; &nbsp; int freq = 0;&nbsp; &nbsp; for(int i = 0; i < word.length(); i++){&nbsp; &nbsp; &nbsp; &nbsp; if((word.charAt(i)) == letter){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; freq++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return freq;}我希望这会有所帮助.. 编码愉快!

临摹微笑

首先,new Scanner当你准备好下一个角色时,你不应该每次都创建。只做一次,之前for loop。第二- 要逐个字符读取扫描仪,您已设置delimeter为"". 在这种情况下,scan.next()返回下一个字符。第三- 你Scanner用来分析字符串,没关系(不是最佳和开销,但没关系)。然后创建新的Scanner实例并依赖它的数据,但不依赖于被治理字符串的长度;使用Scanner.hasNext()方法。我的意思是,您所需要的只是添加hasNext()以确保扫描仪的流中存在更多字符:try (Scanner scan = new Scanner(outputString)) {&nbsp; &nbsp; scan.useDelimiter("");&nbsp; // to make scan.next() return one single character&nbsp; &nbsp; while (scan.hasNext()) {&nbsp; &nbsp; &nbsp; &nbsp; char ch = scan.next().charAt(0);&nbsp; &nbsp; // next() returns String with one character&nbsp; &nbsp; &nbsp; &nbsp; // do your work&nbsp; &nbsp; }}PS这是代码示例,如何用不同的方式计算给定字符串中的字符频率。您可能会发现其中之一与您的任务更相关。// this is your approachpublic static int characterFrequency(String str, char ch) {&nbsp; &nbsp; try (Scanner scan = new Scanner(str)) {&nbsp; &nbsp; &nbsp; &nbsp; scan.useDelimiter("");&nbsp; &nbsp; &nbsp; &nbsp; int count = 0;&nbsp; &nbsp; &nbsp; &nbsp; while (scan.hasNext())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count += scan.next().charAt(0) == ch ? 1 : 0;&nbsp; &nbsp; &nbsp; &nbsp; return count;&nbsp; &nbsp; }}// this one is the most efficientpublic static int characterFrequency(String str, char ch) {&nbsp; &nbsp; int count = 0;&nbsp; &nbsp; for (int i = 0; i < str.length(); i++)&nbsp; &nbsp; &nbsp; &nbsp; count += str.charAt(i) == ch ? 1 : 0;&nbsp; &nbsp; return count;}// this one is the smallest of codepublic static int characterFrequency(String str, char ch) {&nbsp; &nbsp; return (int)str.chars().filter(e -> e == ch).count();}

慕的地8271018

您应该按照上述解决方案获取字符串中的重复字符。但是,我只会提示您为什么会遇到异常考虑以下代码:String outputString = "Pre ";for (int i = 0; i < outputString.length(); i++) {&nbsp; &nbsp; &nbsp; Scanner s = new Scanner(outputString);&nbsp; &nbsp; &nbsp; System.out.println(outputString.length()); // output is 4&nbsp; &nbsp; &nbsp; System.out.println(s.next().length()); //output is 3, not considering the space&nbsp; &nbsp; &nbsp; //char letter = s.next().charAt(i);&nbsp; &nbsp; &nbsp; //System.out.println(letter);&nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java