For your reference, you can do this a few other ways as well.
One way would be by using a method:
import javax.swing.JOptionPane;
public class JunkTesting
{
public static void main(String[] args)
{
String UserInput= JOptionPane.showInputDialog("Enter the sentance: ");
//Run method for both uppercase and lower case chars
int ACount = numberOfTimes(UserInput,'a') + numberOfTimes(UserInput,'A');
int ECount = numberOfTimes(UserInput,'e') + numberOfTimes(UserInput,'E');
int ICount = numberOfTimes(UserInput,'i') + numberOfTimes(UserInput,'I');
int OCount = numberOfTimes(UserInput,'o') + numberOfTimes(UserInput,'O');
int UCount = numberOfTimes(UserInput,'u') + numberOfTimes(UserInput,'U');
System.out.println("The total number of A's are " +ACount);
System.out.println("The total number of E's are " +ECount);
System.out.println("The total number of I's are " +ICount);
System.out.println("The total number of O's are " +OCount);
System.out.println("The total number of U's are " +UCount);
System.out.println("The original sentance was: " +UserInput);
}
public static int numberOfTimes(String s,char c)
{
int count = 0;
for(int i=0;i<s.length();i++)
{
if(s.charAt(i) == c)
count++;
}
return count;
}
}
Another way would be to use one loop:
import javax.swing.JOptionPane;
public class JunkTesting
{
public static void main(String[] args)
{
String UserInput= JOptionPane.showInputDialog("Enter the sentance: ");
//Run method for both uppercase and lower case chars
int ACount = 0;
int ECount = 0;
int ICount = 0;
int OCount = 0;
int UCount = 0;
for(int i=0;i<UserInput.length();i++)
{
if(UserInput.charAt(i)=='a' || UserInput.charAt(i)=='A')
ACount++;
else if(UserInput.charAt(i)=='e' || UserInput.charAt(i)=='E')
ECount++;
else if(UserInput.charAt(i)=='i' || UserInput.charAt(i)=='I')
ICount++;
else if(UserInput.charAt(i)=='o' || UserInput.charAt(i)=='O')
OCount++;
else if(UserInput.charAt(i)=='u' || UserInput.charAt(i)=='U')
UCount++;
}
System.out.println("The total number of A's are " +ACount);
System.out.println("The total number of E's are " +ECount);
System.out.println("The total number of I's are " +ICount);
System.out.println("The total number of O's are " +OCount);
System.out.println("The total number of U's are " +UCount);
System.out.println("The original sentance was: " +UserInput);
}
}
There are tons of other ways as well, those are just to first two that come to mind for me.