首页 文章

使用Scanner在for循环中逐个字母地分析字符串(Throwing IndexOutOfBoundExceptions)

提问于
浏览
1

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

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

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

并使用next()方法使用switch语句分析每个char .

我已经对这些电路板进行了搜索并设计了以下内容: -

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循环创建一个newword字符串来添加每个string.charAt(i)!='') .

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

提前致谢,
凯文

5 回答

  • 0

    我强烈认为你使用这种方法使事情复杂化 . 您可以简单地将String(以及您要搜索的char)传递给方法,如下所示:

    public int checkFreq(char letter, String word){
        int freq = 0;
    
        for(int i = 0; i < word.length(); i++){
            if((word.charAt(i)) == letter){
                freq++;
            }
        }
    
        return freq;
    }
    

    我希望这有帮助..快乐编码!

  • 0

    虽然有这个:

    Scanner s = new Scanner(outputString);
    

    for 循环中,您在每次迭代中都创建了一个新的 Scanner (效率不高,也不是您想要的) .

    如果您已经 String 名为 outputString ,则可以直接访问其字符/字母,如下所示:

    for (int i = 0; i < outputString.length(); i++) {
        char letter = outputString.charAt(i);
        //The rest of you code here
    }
    
  • 0

    这是在另一个问题上提供的另一种解决此问题的方法 . (不能留下评论..所以必须作为答案......)How to count frequency of characters in a string?

  • 0

    首先,每次准备下一个角色时,你都不会创建 new Scanner . 在 for loop 之前只做一次 .

    第二 - 要逐字符地读取扫描仪,您已将 delimeter 设置为 "" . 在这种情况下, scan.next() 将返回下一个字符 .

    第三 - 你使用 Scanner 来分析字符串,这没关系(不是最优和开销,但还可以) . 然后创建新的 Scanner isntance并依赖它的数据,但不依赖于goven字符串的长度;请使用 Scanner.hasNext() 方法 . 我的意思是你需要的只是添加 hasNext() 以确保扫描程序的流中存在更多字符:

    try (Scanner scan = new Scanner(outputString)) {
        scan.useDelimiter("");  // to make scan.next() return one single character
    
        while (scan.hasNext()) {
            char ch = scan.next().charAt(0);    // next() returns String with one character
            // do your work
        }
    }
    

    P.S. 这是代码示例,如何以不同的方式在给定字符串中保持字符频率 . 也许,其中一个你会发现你的任务更相关 .

    // this is your approach
    public static int characterFrequency(String str, char ch) {
        try (Scanner scan = new Scanner(str)) {
            scan.useDelimiter("");
            int count = 0;
    
            while (scan.hasNext())
                count += scan.next().charAt(0) == ch ? 1 : 0;
    
            return count;
        }
    }
    
    // this one is the most efficient
    public static int characterFrequency(String str, char ch) {
        int count = 0;
    
        for (int i = 0; i < str.length(); i++)
            count += str.charAt(i) == ch ? 1 : 0;
    
        return count;
    }
    
    // this one is the smallest of code
    public static int characterFrequency(String str, char ch) {
        return (int)str.chars().filter(e -> e == ch).count();
    }
    
  • 0

    您应该按照上面的解决方案获取字符串中的重复字符 . 但是,我只会给你一个关于你为什么得到例外的提示

    考虑以下代码:

    String outputString = "Pre ";
    for (int i = 0; i < outputString.length(); i++) {
          Scanner s = new Scanner(outputString);
          System.out.println(outputString.length()); // output is 4
          System.out.println(s.next().length()); //output is 3, not considering the space
          //char letter = s.next().charAt(i);
          //System.out.println(letter);
      }
    

相关问题