To count the one letter in a word

So basically, I have to write a code for a coding class the program where the user inputs a word and outputs the location of one specific letter.

So for example: If the user input "america" it would output "1" "a"

So basically the output I was expecting was

Enter any Word :  America

Letter? :  a

1

Well instead, the code I ended up writing counts for all the specific letter so basically...

Enter any Word :  america
Letter? :  a

2

Here's my code:

import java.util.*;

class Main {
  public static void main(String[] args) {
    
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter any Word : ");
   
    String Loki = sc.next().toLowerCase();
    System.out.print("Letter? : ");
   
    char Thor = sc.next(".").toLowerCase().charAt(0); 
    int count = 0;
    
    for (char a : Loki.toCharArray()) {
    if (Thor == a) {
      
      count++;
    }
    }
        System.out.println("\n"+count);
    
    
  }
}

And before you ask I was watching Thor Ragnarok and that's why I named the variables the way they I did



Read more here: https://stackoverflow.com/questions/64938962/to-count-the-one-letter-in-a-word

Content Attribution

This content was originally published by Linux2 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: