Finding occurrence of a given character in a String? I came up with this simple solution:

public class Main {

  /**
   * How to count occurrence of a given character in a String?
   */

  public static void main(String[] args) {

    Scanner in = new Scanner(System.in);

    if (!in.hasNextLine()) {
      System.out.println("You must enter a string!");
    }

    String givenString = in.nextLine();

    char givenChar = in.next().charAt(0);

    int occurance = occuranceOfChar(givenString, givenChar);

    System.out.println(givenChar + " occured " + occurance + " times!");
  }

  private static int occuranceOfChar(String givenString, char givenChar) {

    int sum = 0;

    for (int i = 0; i &lt; givenString.length(); i++) {

      char charAtI = givenString.charAt(i);

      if (charAtI == givenChar) sum++;
    }

    return sum;
  }
}


Are there any bad practices here?
Your program doesn't print a prompt to the user and I couldn't make it output the warning to enter a string. I have made a few basic changes. The actual counting is done in a very basic but robust way 

public class Main {

    /**
     * How to count occurrence of a given character in a String?
     */

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        System.out.println("Please enter a string to search");

        String givenString = in.nextLine();

        System.out.println("Please enter the character to search for");
        char givenChar = in.next().charAt(0);

        int occurance = occuranceOfChar(givenString, givenChar);

        System.out.println(givenChar + " occured " + occurance + " times!");
    }

    private static int occuranceOfChar(String givenString, char givenChar){

        int sum = 0;

        for (int i = 0; i &lt; givenString.length(); i++) {

            char charAtI = givenString.charAt(i);

            if (charAtI == givenChar) sum++;
        }

        return sum;
    }
}