First, you should indent and space out all of your code properly so it's readable. Observe below, I will make no change to your code except to add proper spacing:
public class Assignment
{
public static char toUpperCase(char ch)
{
char result;
if (ch >= 'a' && ch <= 'z')
{
result = (char) (ch - 32);
}
else
{
result = ch;
}
return result;
}
public static void main(String[] asd)
{
String s ="My country tis of thee, Sweet land of liberty, Of thee I sing";
int[] bin = new int[26];
for(int j = 0; j < s.length(); j++)
{
char ch = s.charAt(j);
int digit = ch -'0';
bin[digit]++;
}
for(int k = 0; k < 27; k++)
{
System.out.print("# of " + k + "'s = " + bin[k]+"|" );
for(int j = 0; j < bin[k]; j++)
{
System.out.print(k);
System.out.println();
}
}
}
You'll notice that you're missing a bottom bracket, and that's why you're having problems. Well, there are other problems, but that's the compilers problem with your code. :P
Second, this is bad:
public static char toUpperCase(char ch)
{
char result;
if (ch >= 'a' && ch <= 'z')
{
result = (char) (ch - 32);
}
else
{
result = ch;
}
return result;
}
Bad, bad, bad, bad, bad, bad, bad. Never do this, first off you're assuming ASCII, and since Java pretty much exclusively uses Unicode, that's not a safe assumption. Also, this function is entirely unnecessary, because strings already come with a toUpperCase() method! All you need to do is this:
s = s.toUpperCase();
and you're done. There's nothing else to it, get rid of that function.
Finally, the way you're getting the characters is wrong. While using two separate loops to count and to print is good, the way they're structured isn't very good. Instead, you should have a first loop that counts using a simple mathematical algorithm over each character (and has to ignore characters that are not within the alphabet) and the second that just prints it out. This is how I'd do the first loop:
for(int iii = 0; iii < s.length(); ++iii)
{
int theChar = s.charAt(iii);
if (theChar < 'A' || theChar > 'Z') continue;
bin[theChar - 'A']++;
}
The for loop and the assignment should be pretty self-explanatory. What the if statement is doing is determining if any character that comes from s.charAt() is less than 'A' or greater than 'Z' (Assuming that 'A'++ == 'B', and 'B'++ == 'C', etc. is a safe assumption) and if it is, then to skip it. The third line is executed only if the if statement returned false, which means that the character is within range. What happens is you just take theChar and subtract 'A'. Because of the above assumption, you can also assume that 'A' - 'A' = 0, and that 'B' - 'A' == 1, 'C' - 'A' == 2, etc. This is an easy mathematical way to figure out where to put the letter in the array, and doesn't assume a specific value for 'A'.
The second loop I built like this:
for(int iii = 0; iii < 26; ++iii)
{
System.out.println("# of " + (char) (iii + 'A') + "'s = " + bin[iii]);
}
Other than the for loop, it's just one line of code. It prints out as a line, the combination of the string "# of ", the value of iii + 'A' cast as a char, the string "'s = ", and the value of bin[iii]. This should result in strings like "# of A's = 1" or whatever value accounts for the number of A's.
This whole thing is much more concise and easier to read, while also making no assumptions about character encoding or what string is being input. For example, you could even input an entire text file into this program and count the number of alphabetical characters and what they are in this program. This is much simpler. :)