Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dereferencing and int

 
Rory Rezzelle
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9279
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rory Rezzelle
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Rory Rezzelle
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9279
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. :-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic