0

I need help on my code in Java.

This is the problem :

Example input : AaaaaAa

Output : A appears 7.

The problem is I need it to ignore cases.

Please help me, my code works fine, except that it doesn't ignore cases.

import java.io.*;

public class letter_bmp{
    public static BufferedReader input = new BufferedReader( new InputStreamReader(System.in));
    public static void main(String[] args) throws Exception

    {

    String string1;
    String pick;

    String ans;
    do
    {
    int count=0;
    System.out.print("En taro Adun, Executor! Input desired string : ");
    string1 = input.readLine();
    System.out.print("Now, Executor...which character shall I choose : ");
    pick = input.readLine();

    for(int counter = 0; counter < string1.length(); counter++)
        {
        if(pick.charAt(0) == string1.charAt(counter))
        count++;
        }
    System.out.print("Executor...you picked '" + pick + "' it is used " + count + " times in the word "+string1+".");

    System.out.println("\nWould you like to try again, Executor? (Yes/No): ");
    ans = input.readLine();
    }
    while(ans.equalsIgnoreCase("Yes"));
    }

}
1
  • Ohh, I didn't see that I put something like that there. Commented Mar 6, 2014 at 12:22

3 Answers 3

1

Convert the string to lower case characters using the String.toLowerCase() method.

// ...
string1 = input.readLine().toLowerCase();
// ...
pick = input.readLine().toLowerCase();
// ...
Sign up to request clarification or add additional context in comments.

Comments

0

The easiest solution is to make 2 new strings like this:

string1_lower = string1.toLowerCase();
pick_lower = pick.toLowerCase();

And use those two variables during comparison.

Comments

0

I understand that the question is old and the OP might have got his answer. But i am putting this out here just in case if anybody needs it in the future.

public static void main(String[] args) {
    String s="rEmember";

    for(int i = 0; i <= s.length() - 1; i++){
        int count = 0;
        for(int j = 0; j <= s.length() - 1; j++){
            if(Character.toLowerCase(s.charAt(i)) == Character.toLowerCase(s.charAt(j))){
                count++;
            }
        }
        System.out.println(s.charAt(i) + " = " + count + " times");
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.