aspose file tools*
The moose likes Beginning Java and the fly likes Counting uppercase? Part 2! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Counting uppercase? Part 2!" Watch "Counting uppercase? Part 2!" New topic
Author

Counting uppercase? Part 2!

Stacey Johnson
Ranch Hand

Joined: Jan 11, 2004
Posts: 55
Hi,
I'm now working on a program that counts all the uppercase letters in a string entered in the command line arguement. When I try to compile I get a couple errors. I'm not too sure what the problem is. Can anyone help? In my last message a comment was made "The call to "isUpperCase()" should be "toUpperCase()". But it is my understanding that isUpperCase method is true if the letters are uppercase only, toUpperCase changes all the letters to uppercase letters. If I'm understanding this wrong, please let me know. Below is my code and after that the two errors I keep getting.
/**import JOptionPane (extention package) to support swing*/
import javax.swing.JOptionPane;
public class Exercise7_7 {
/** Main method */
public static void main(String[] args) {

//Check command-line argument
if (args.length !=1) {

JOptionPane.showMessageDialog(null, "You did not enter a string",
"Exercise 7.7, TME 2", JOptionPane.INFORMATION_MESSAGE);

System.exit(0);
}

// Invoke the countLetters method to count each uppercase letter
int[] count = countLetters(word.isUpperCase());

// Display results
for (int i = 0; i < count.length; i++) {
if (count[i] != 0)
System.out.print((char)('a' + i) + " appears " +
count[i] + ((count[i] == 1) ? " time\n" : " times\n"));
}
// Count each letter in the string
public static int[] countLetters(String word) {
int[] count = new int[26];

for (int i = 0; i < word.length(); i++) {
if (Character.isLetter(word.charAt(i)))
count[(int)(word.charAt(i) - 'a')]++;
}

return count;
};
}
}

ERRORS:
illegal start of expression
public static int[] countLetters(String word) {
^
cannot resolve symbol
symbol : variable word
location: class Exercise7_7
int[] count = countLetters(word.isUpperCase());
^
Thanks in advance
Stacey
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Hi Stacey
The compiler is complaining that the line:
public static int[] countLetters(String word) {
is an "illegal start of expression". These can be tricky errors to track down. The first key word here is "start" - it is telling us that the compiler is not happy with the start of this expression; this tells us the issue is either with the start of this line, or possibly occurs before it. The other key word is "expression". The line you have is not an expression. Expressions are things like x=5 or System.out.println() - what you have is a method declaration. So we now know that the compiler is viewing this as an expression and not a method declaration. So your job is to figure out why. Think about the fact that expressions (typically) appear inside a method, and it may help you track down the issue; the compiler is seeing " public static int[] countLetters(String word)" as occurring inside a method. Take a look at your code below after the code is probably indented (based on what you have written.) and it might also help you spot the error. And related to that, it would be very helpful if when posting your questions you posted your code inside [code] tags � this will keep the code�s indentation and use a mono-spaced font - this will help people to read your code and spot issues faster. If you are not sure how to use the [code] [/code] tags, take a look at the info shown here.


And if you are still having trouble figuring out where the issue is, take a look at this post and read my comments on the discussion at hand and one of the reasons why I am of a proponent of the position I take. It may help you figure out the issue. ;)
[ February 13, 2004: Message edited by: Mark Vender ]
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

As for your second error:
cannot resolve symbol
symbol : variable word
This error says that the compiler cannot resolve the variable "word" - keep in mind you must declare a variable before using it Take a look at your code and with this knowledge you should see the issue.
Stacey Johnson
Ranch Hand

Joined: Jan 11, 2004
Posts: 55
Mark,
Thank you for your replies, but I am still racking my brains. I've read your additional posts and I stare at it and don't know where to go. Again this whole command line is throwing me off. For example I know "word" should be declared as a variable but I really don't know how and where. I haven't worked with strings all that much yet so I'm a bit confused. As for my method being recognized as an expression I just can't see the problem. I don't now if int[] is it or what. Can you give me a bit more of a hint please I'm still very Java inept .
Thanks in advance,
Stacey
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Originally posted by Stacey Johnson:
Hi,
In my last message a comment was made "The call to "isUpperCase()" should be "toUpperCase()". But it is my understanding that isUpperCase method is true if the letters are uppercase only, toUpperCase changes all the letters to uppercase letters. If I'm understanding this wrong, please let me know.
Thanks in advance
Stacey

You are correct. toUpperCase() is a method that converts all characters in a String to uppercase. isUpperCase() returns true if a char is upper case. Since you are calling

you are calling it on a string. There is no isUpperCase() method in the String class. There is isUpperCase() method that is part of the Character wrapper class. It is a static class, which means you do not call it on an instance of Character. Therefore, I can write this:

So, knowing that a String is basically an array of chars, you need to look at the String char by char, calling the Character.isUpperCase(char ch) - take a look at the String class to determine any methods you need to get a specific char in a String use that in a loop.
Try that; I hope it helps, and if not, let us know where you are hung up and someone can help you out.
Regards,
Mark
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Originally posted by Stacey Johnson:
Mark,
Thank you for your replies, but I am still racking my brains. I've read your additional posts and I stare at it and don't know where to go. Again this whole command line is throwing me off. For example I know "word" should be declared as a variable but I really don't know how and where. I haven't worked with strings all that much yet so I'm a bit confused. As for my method being recognized as an expression I just can't see the problem. I don't now if int[] is it or what. Can you give me a bit more of a hint please I'm still very Java inept .
Thanks in advance,
Stacey


Sure... First let�s look at the first error. Look at the code now when I remove all the "guts" and leave just the skeleton:


If look at the countLetters(String) method now, you might notice that it doesn�t look like it�s in the right place. While there are the correct number of braces, they are in the wrong place. Since you said you are not use to command line programs, the reason you might not see the error is you might not be aware that you do not place other methods inside the main method. The main is not any different (in terms of syntax) then any other method. You must place the countLetters() method as a separate method:

Take a look at that and work on fixing the first error. I�ll answer your other questions in a moment...
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Ok, now on to your second issue/question about how do you declare the word variable.
Command line programs can receive a series of arguments (a.k.a. "args") when the program is launched. The main method receives these arguments as an array of Strings. So if I use the command:
java SomeClass argument_A argument_B argument_C
The public static void main(String[] args) "receives" those command line arguments as elements of the args array. So if my SomeClass program did this and was run using the above command line:

the output would be:
Argument at index 0 is: argument_A
Argument at index 1 is: argument_B
Argument at index 2 is: argument_C
Keep in mind I could have just as easily said:

and gotten the same results and it really doesn't make a huge difference in this contrived program. However command line arguments usually have a purpose or meaning, args [i] is meaningless to anyone that reads the code (so is the "arg" variable I used above). It is general a good programming practice to store that command line argument in a variable with a meaningful name, as we are about to do for your program...
So, if I want to run your program by saying:
java Exercise7_7 someWordToCount
so that it counts the uppercase letters in someWordToCount, we know that the main method will store that a word in the first element of the array args. Since arrays are zero based, the first element is stored in index 0.
So we get:
String word = args[0];
Then you can call various methods on your String "word"
[ February 13, 2004: Message edited by: Mark Vender ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Counting uppercase? Part 2!