Count the occurrences of a letter in a string

前端 未结 10 1527
礼貌的吻别
礼貌的吻别 2021-01-21 03:42

I am trying to write a for loop in Java that will count the occurrences of a letter in a string. The user will enter the letter to count and the string in which to search. This

相关标签:
10条回答
  • 2021-01-21 03:50

    Try this

    forget String letter = "" <-- Delete
    forget letter = in.next() <-- Delete

        // There's no nextChar() method, so this is a work aroung
        char ch = in.findWithinHorizon(".", 0).charAt(0);
    
        int letter = 0;
        for (int i = 0; i < sentence.length(); i++) {
    
            if (sentence.charAt(i) == ch) {
                letter++;
            }
        }
    
        System.out.println(letter);  // print number of times letter appears
    
        // You don't want this
        System.out.print(sentence.charAt(letter)); // Makes no sense
    
    0 讨论(0)
  • 2021-01-21 03:58

    your Scanner class has not moved to the next line after reading the character

    letter = in.next().charAt(0);
    

    add another in.nextLine() before reading the input string

        System.out.println("Enter a character for which to search");
        letter = in.next().charAt(0);
        in.nextLine();
        System.out.println("Enter the string to search");
        sentence = in.nextLine();
    

    old thread but hope this helps :)

    0 讨论(0)
  • 2021-01-21 04:02

    Your if (sentence.length() <= 0) { is not right. Change your condition like:

    System.out.println("Enter a character for which to search");
    letter = in.next();
    System.out.println("Enter the string to search");
    sentence = in.next();
    char searchLet=letter.charAt(0); // Convert String to char
    int letter = 0;
    for (int i = 0; i < sentence.length(); i++) {
        char ch = sentence.charAt(i);
        if (searchLet== ch) {  // Check the occurrence of desired letter.
            letter++;
        }
    }
    
    System.out.print(sentence.charAt(letter));
    
    0 讨论(0)
  • 2021-01-21 04:02
    if (sentence.length() <= 0) {
                letter++;
    }
    

    The above part of code in your program is wrong. This will never be true until otherwise you input an empty string.

    And basically this is not the correct logic. You will have to use the direct comparison.

    0 讨论(0)
  • 2021-01-21 04:02

    Try this:

    Char letter = '';
    String sentence = "";
    System.out.println("Enter a character for which to search");
    letter = in.next().charAt(0);
    System.out.println("Enter the string to search");
    sentence = in.next();
    
    int count= 0;
    for (int i = 0; i < sentence.length(); i++) {
        char ch = sentence.charAt(i);
        if (ch==letter) {
            count++;
        }
    }
    System.out.print(letter+" occurance:"+count);
    
    0 讨论(0)
  • 2021-01-21 04:04

    I see a couple of issues. First you have two variables with the same name.

    Second your if condition check for the lenght of the sentence to be greater then 0 instead of checking for character equality.

    Scanner in = new Scanner(System.in);
    
    char inLetter = "";
    String sentence = "";
    System.out.println("Enter a character for which to search");
    inLetter = in.next().charAt(0);
    System.out.println("Enter the string to search");
    sentence = in.next();
    
    int letter = 0;
    for (int i = 0; i < sentence.length(); i++) {
        char ch = sentence.charAt(i);
        if (inLetter == ch) {
            letter++;
        }
    }
    
    System.out.print(sentence.charAt(letter));
    

    I would also strongly suggest to validate the input (which is not done in the example above) instead of just assuming you got 1 character from the first input and 1 sentence in the second.

    0 讨论(0)
提交回复
热议问题