Sorry to ask about a homework assignment, but I'm at my wits end.
I have a two part assignment.
The first part was to, using indexOf, ask the user for a string and then ask for a character. Then search the string and count how many times that character appears in the string.
So if the string was "hello"
and the character was "l"
The output would be 2.
This I've got and I'll post the code below.
My problem is the second half of the exercise which is:
Create another app based on the First one that asks a user for a string. Take that string and compare it against the alphabet and count how many letters of the alphabet appear in the string.
So here would be a sample output:
if the string was "abc"
output would be
a = 1
b = 2
c = 3
d = 0
e = 0
f = 0
g = 0
and so on to z = 0.
I've probably rewritten this code a million times. I think I might be able to do it with an if statement and a variable for each letter of the alphabet, but I just don't accept that as the solution. I know it should be easier.
Any hints, suggestions or pointers would be great. Again, sorry to ask about homework, I'm just not sure where to go from here.
Thank You all!
public class Index
private static Component frame;
public static void main(String args)
String s = JOptionPane.showInputDialog("Please Enter a String");
String c = JOptionPane.showInputDialog("please Enter a Charecter");
int count = 0;
int index = 0;
while((index = s.indexOf(c, index )) >= 0 )
JOptionPane.showMessageDialog(frame,"There are "+count +" of those charecters in your string.");