I also need the char output to be upper case.
Now :
a:
5 times 5 times
25% frequency
What I need:
A:
5 times
25% frequency
I did the main thing, but now I want the program to end when the user enters a space or an invalid character. Is there anyway for me to stop at two decimal places? Any suggestions?
import java.util.*; // For Standard Input Scanner class
import java.text.*; // For decimal format DecimalFormat class
public class count {
// Constants
private final int MAX = 27;
private int [] lower = new int [MAX];
public static void main(String[] args) {
// initialzes scanner and private instance variables/object
Scanner keyboard = new Scanner(System.in);
char current;
final int MAX = 27;
int [] lower = new int [MAX];
int Spaces = 0;
System.out.println("Enter a sentence:");
String line = keyboard.nextLine ();
//converts the string into an int
line = line.toLowerCase();
//sets requirements for the array and if/else statements
for (int ch = 0; ch < line.length(); ch++)
{
current = line.charAt(ch);
if (current >= 'a' && current <= 'z')
lower[current -'a']++;
else if (current == ' ')
Spaces ++;
}
System.out.println();
//sets the requirement for the 2nd array and prints occurence
//and the frequency (percent)
for (int letter = 0; letter < lower.length;letter++)
{
if (lower[letter]>0)
{
System.out.print((char)(letter + 'a'));
System.out.println();
System.out.println(" count = " + lower[letter] );
System.out.println(" percentage = " +
(float)lower[letter] * 100/(line.length()) + "%");
System.out.println();
}
}
//counts how many spaces
System.out.println ();
System.out.println ("But had " + Spaces + " spaces: ");
}
}