This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Dereferencing and int Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Dereferencing and int" Watch "Dereferencing and int" New topic
Author

Dereferencing and int

Rory Rezzelle
Greenhorn

Joined: Sep 30, 2003
Posts: 5
I'm trying to finish a little program for class today and I get this error: int cannot be dereferenced
Now, I know what it's telling me, but how can I change it?
Here's what I'm doing, I'm making a program to count how many times the user types a vowel in a String and then outputting it to JTextArea.
Here's the loop where I get the error. I put the carrot in bold to show where I'm getting the error at exactly (should be under the "." in ui_length.charAt(i)):
String user_input;
int ui_length;
int i = 0;
int char_a = 0;
int char_e = 0;
int char_i = 0;
int char_o = 0;
int char_u = 0;
for (ui_length = user_input.length(); i < ui_length; i++)
{
char ch = ui_length.charAt(i);
^
switch (ch)
{
case 'a':
case 'A': char_a++;
case 'e':
case 'E': char_e++;
case 'i':
case 'I': char_i++;
case 'o':
case 'O': char_o++;
case 'u':
case 'U': char_u++;
}
}
Could someone please help me?
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

Originally posted by Rory Rezzelle:

String user_input;
int ui_length;

for (ui_length = user_input.length(); i < ui_length; i++)
{
char ch = ui_length.charAt(i);
^
Could someone please help me?

One may ask why you are trying to call charAt() on an integer.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Rory Rezzelle
Greenhorn

Joined: Sep 30, 2003
Posts: 5
I was hoping it would just allow me to use "i" so that my program could use the for loop to go through the string and count the vowels.
Is this not how I would do that?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

There are several weird things going on here; I think you may have just got yourself confused.

First, that "for" loop. The counter variable is "i", but you've initialized it before the loop; and you've initialized the variable "ui_length" there instead. Although there's nothing incorrect, it's unusual code, and it would be clearer if you even just wrote

But actually, HotSpot is pretty smart, and it knows that Strings are immutable, and writing the loop in the most straightforward way is actually going to get you identical performance:

Now, on to the error: I think you just mean to get the i'th character of user_input, not the i'th character of ui_length:


[Jess in Action][AskingGoodQuestions]
Rory Rezzelle
Greenhorn

Joined: Sep 30, 2003
Posts: 5
Originally posted by Ernest Friedman-Hill:

That helps with that problem... but now when I type in a string it counts extra vowels. IE: I typed in "hello" and I got a count of 2 u's.
Any help?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Well, you didn't ask me to debug your whole program, only to tell you why it wasn't compiling.
Each "case" of a switch statement needs to be terminated by a "break" statement, or otherwise the cases just run together. So given your code as written, when there's an "e", you'll increment char_e, char_i, char_o and char_u. char_u is 2 because there arr two vowels in the word, and both the "e" and "o" cases will fall through. To fix this you have to say

etc.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

Originally posted by Rory Rezzelle:
<hr></blockquote>
That helps with that problem... but now when I type in a string it counts extra vowels. IE: I typed in "hello" and I got a count of 2 u's.
Any help?

The
Java Tutorial knows all.
Rory Rezzelle
Greenhorn

Joined: Sep 30, 2003
Posts: 5
Thanks. :-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dereferencing and int
 
Similar Threads
Count the number of vowels, words, and sentences.
method that reads a string for vowels?
Loop With Switch
String Array
Please help, java program terminating unexpectedly without reason