Note that there are some explanatory texts on larger screens.

plurals
  1. POHow many times the first character appears in a string
    primarykey
    data
    text
    <p>What I am Trying to Achieve:</p> <p>Write a program that will ask the user to input a String. Then output the number of times the first letter of that String occurs. Assume the user will enter a String with all uppercase letters. For example, if the user enters “PETER PIPER PICKED A PECK OF PICKLED PEPPERS” your program should output “P occurs 9 times”.</p> <p>What I have so far:</p> <pre><code>import javax.swing.JOptionPane; public class counterCharacter { public static void main(String[] args) { String userInput = JOptionPane.showInputDialog("Input a string"); int count = userInput.indexOf(0); for(int i =0; i &lt; userInput.length(); i++) if(userInput.charAt(i) == 'a') count++; System.out.println(count); } } </code></pre> <p>The problem with this code is that it only prints the character 'a' and only if it occurs in index point 0. My task is to allow a user to input any string, take the character from index point 0 of the users input, and count exactly how many times that character reoccurs within the input. For the PETER PIPER example, the system should print out "6" because that is how many times the character in the index 0 position occurs. But if the user input abcdcbabcdcda, it should print out "3." </p> <p>Any help with this would be much appreciated as I am a new programmer and missed a day of class and am now far behind and trying to catch up. If you can, please explain why you used the specific code you used and any reference materials I can use to learn how to use that specific thing. Thank You! </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload