1

So first of all, hello everyone this is my first time stackoverflow as a question asker and I know you folks don't like people asking homework questions on here but I've been struggling with this for about a week now and have given it several reasonable attempts so I actually need help here and am not just trying to mooch answers off you amazing coders :)

So my task at hand is I'm trying (the language is java btw) to find the number of times a letter (which the user inputs) occurs in a word (which the user also picks, and then to output the number of time that word occurs, for example: the word hello has two 'l's in it.. it should be pretty easy but for some reason I can't get it :/

I believe using my current code the variable "let" gets turned into an ascii character and idk what to do with that, or rather how I should compare it with all the other characters in the word. Please help :)

import java.util.Scanner;


public class LetterCounter {

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    String word = "";
    String letter;
    int limit = 0;
    String input = null;
    String let;
    int count = 0;
    int j = 0;

    while(limit == 0){
        System.out.println("Type a word.");
        word = scan.nextLine();
        System.out.println("Type a single letter.");
        letter = scan.nextLine();
        let = letter.substring(0,1);

        char car;
        while(j<word.length()){
            car = word.charAt(j);
            for(int x=1; x==j; x++){
                if(let.charAt(0)==car){ 
                    count ++;
                }
            }

            j+=1;
        }
            System.out.println(count + " " + "occurances.");
        }
   }
 }
Madushan Perera
  • 2,568
  • 2
  • 17
  • 36
  • 1
    How do you think your code should work? And what is the purpose of `int mike = car = word.charAt(j);`? – Pshemo Nov 28 '15 at 20:41
  • @Pshemo I think I used it in a previous attempt at solving the question and didn't delete it. I will edit it. Thanks – For the love of binary help me Nov 28 '15 at 20:43
  • 2
    Possible duplicate of [How do I count the number of occurrences of a char in a String?](http://stackoverflow.com/questions/275944/how-do-i-count-the-number-of-occurrences-of-a-char-in-a-string) – ΦXocę 웃 Пepeúpa ツ Nov 28 '15 at 20:44
  • 4
    Please take your time and post your best attempt. All you need to do is (1) get data from user (you already have it) (2) iterate over all characters in word (3) when iterating compare that character with character from user: if it is same increase your counter. – Pshemo Nov 28 '15 at 20:46

1 Answers1

1

Here is a sample code that should work

import java.util.Scanner;

public class LetterCounter {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        System.out.println("Type a word.");
        String word = scan.nextLine();
        System.out.println("Type a single letter.");
        String letter = scan.nextLine();
        char let = letter.charAt(0);

        int count = 0;
        for (char char1: word.toCharArray()) {
            if (char1 == let) {
                count++;
            }

        }
        System.out.println(count + " " + "occurrences.");
    }
}

Here is the test output

Type a word.
letter
Type a single letter.
t
2 occurrences.