查找给定字符串中每个字符的出现

“我想查找并打印给定字符串中每个字符的出现,我已经建立了自己的逻辑,但存在一些问题。例如,如果我将输入作为“ JAVA”输入。 我的程序产生的输出将是


J 1

A 2

V 1

A 1

预期输出:


    J 1

    A 2

    V 1

我不想再打印 A 了。我希望你们都能明白我的代码中有什么问题。”


导入 java.util.Scanner;


公共类 FindOccuranceOfCharacter {


public static void main(String[] args) {

    // TODO Auto-generated method stub

    String x;

    Scanner input = new Scanner(System.in);

    System.out.println("Enter a string");

    x = input.nextLine();

    x = x.toUpperCase();

    int size = x.length();

    for(int i =0;i<size;i++) {

        int count=1;

        char find = x.charAt(i);


        for(int j=i+1;j<size;j++) {

            if(find == x.charAt(j)) {

                count++;

            }

        }


        System.out.printf("%c\t%d",x.charAt(i),count);

        System.out.println();       

    }

}

}


holdtom
浏览 139回答 3
3回答

万千封印

您的代码以这种方式打印的原因是您的循环打印给定索引的每个字符(和后续匹配项)。你真的需要用一个循环将字符和计数存储在一个数据结构中,然后用第二个循环显示计数。ALinkedHashMap<Character, Integer>非常适合您的用例(因为它保留了键插入顺序,不需要额外的逻辑来恢复输入顺序)。我将进行的其他更改包括使用String.toCharArray()和for-each循环。Map<Character, Integer> map = new LinkedHashMap<>();for (char ch : x.toUpperCase().toCharArray()) {    map.put(ch, map.getOrDefault(ch, 0) + 1);}for (char ch : map.keySet()) {    System.out.printf("%c\t%d%n", ch, map.get(ch));}我用xequal测试JAVA并得到了(按要求)J   1A   2V   1喜欢,

森林海

使用 hashMap 可以很容易地累积出现的次数,并且可以轻松地打印迭代 HashMap。这是代码:public class FindOccuranceOfCharacter {public static void main(String[] args) {&nbsp; &nbsp; String x;&nbsp; &nbsp; Scanner input = new Scanner(System.in);&nbsp; &nbsp; System.out.println("Enter a string");&nbsp; &nbsp; x = input.nextLine();&nbsp; &nbsp; HashMap<Character,Integer> occurance = new HashMap<Character,Integer>();&nbsp; &nbsp; x = x.toUpperCase();&nbsp; &nbsp; int size = x.length();&nbsp; &nbsp; for(int i =0;i<size;i++) {&nbsp; &nbsp; &nbsp; &nbsp; int count=1;&nbsp; &nbsp; &nbsp; &nbsp; char find = x.charAt(i);&nbsp; &nbsp; &nbsp; &nbsp; occurance.put(find, occurance.getOrDefault(find, 0) + 1);&nbsp; &nbsp; }&nbsp; &nbsp; for (Character key : occurance.keySet()) {&nbsp; &nbsp; &nbsp; &nbsp; Integer value = occurance.get(key);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Key = " + key + ", Value = " + value);&nbsp; &nbsp; }&nbsp; &nbsp;}

回首忆惘然

这不是最佳解决方案,但我已尝试尽可能少地更改您的代码:public static void main(String args[]) {&nbsp; &nbsp; Scanner input = new Scanner(System.in);&nbsp; &nbsp; System.out.println("Enter a string");&nbsp; &nbsp; // use a StringBuilder to delete chars later on&nbsp; &nbsp; StringBuilder x = new StringBuilder(input.nextLine().toUpperCase());&nbsp; &nbsp; for(int i=0;i<x.length();i++) {&nbsp; &nbsp; &nbsp; &nbsp; int count=1;&nbsp; &nbsp; &nbsp; &nbsp; char find = x.charAt(i);&nbsp; &nbsp; &nbsp; &nbsp; // go through the rest of the string from the end so we do not mess up with the index&nbsp; &nbsp; &nbsp; &nbsp; for(int j=x.length()-1;j>i;j--) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(find == x.charAt(j)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // delete counted occurences of the same char&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x.deleteCharAt(j);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("%c\t%d",x.charAt(i),count);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; }}我更喜欢的Java 流如下所示:input.nextLine().toUpperCase().chars()&nbsp; &nbsp; &nbsp; &nbsp; .mapToObj(i -> (char) i)&nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting()))&nbsp; &nbsp; &nbsp; &nbsp; .forEach((k, v) -> System.out.println(k + "\t" + v));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java