1
System.out.print("Enter a character: ");
        String userInput= keyb.next();
        char i = userInput.charAt(0); //getting the character by itself
        int counter=0;
        for(int index= 0; index < theString.length(); index++)
        {
            char ch = userInput.charAt(index);
            if (ch==i) //comparing the chosen character to each character in the string
                counter++; //keeping track of how many times you find a match

I am brand new to programming and I have to write a program that will count the number of occurrences of a character chosen by the user in a string that is also an input. This is just the part of the program that has the problem, the error I get when running is: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1. Not sure what I'm doing wrong!

Miki
  • 2,493
  • 2
  • 27
  • 39
rayray
  • 21
  • 4
  • Post the rest of your code you probably need to change userInput.charAt(index) to theString.charAt(index); – brso05 Oct 14 '14 at 20:44
  • 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) – Miki Oct 14 '14 at 20:59

4 Answers4

1
    for(int index= 0; index< theString.length(); index++)

Where does theString come from? I suspect you mean userInput.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • theString is the original string entered at the start of the program. It is what I am trying to count the characters in – rayray Oct 14 '14 at 20:45
1

Change To:

System.out.print("Enter a character: ");
        String userInput= keyb.next();
        char i = userInput.charAt(0);//getting the character by itself
        int counter=0;
        for(int index= 0; index< userInput.length(); index++)
        {
            char ch = userInput.charAt(index);
            if (ch==i)//comparing the chosen character to each character in the string
                counter++;//keeping track of how many times you find a match

you get the out of range because you are not iterating over the user input.

Eddie Martinez
  • 13,582
  • 13
  • 81
  • 106
  • Alternatively, change to `char ch = theString.charAt(index);`. I can't tell which is actually wanted. – John Bollinger Oct 14 '14 at 20:46
  • this fixed the error but now it won't print the right counter value. It says one no matter the input. ex apples has only one p in it – rayray Oct 14 '14 at 20:53
1
System.out.print("Enter a character: ");
        String userInput= keyb.next();
        char i = userInput.charAt(0);//getting the character by itself
        int counter=0;
        for(int index= 0; index< theString.length(); index++)
        {
            char ch = **theString**.charAt(index);
            if (ch==i)//comparing the chosen character to each character in the string
                counter++;//keeping track of how many times you find a match

My assumption is that you want to loop through theString comparing each letter of that to the original char. Remove the ** from theString I just added it to draw your attention to the change. I'm guessing theString is defined elsewhere in your code.

brso05
  • 13,142
  • 2
  • 21
  • 40
-1

Check the line number in the stack trace and then debug.

sumanta
  • 359
  • 3
  • 7