File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes why doesnt my code work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "why doesnt my code work" Watch "why doesnt my code work" New topic
Author

why doesnt my code work

Ricci Dolan
Greenhorn

Joined: Jul 09, 2006
Posts: 5
Im doing a DIY Javascript book and one of the problems i cant seem to solve involves the following code


any help as to why this doesnt count the numbers of "a"'s in a string would be very much appriciated, im pretty sure its somthing small.

thanks in advance
[ July 09, 2006: Message edited by: Ricci Dolan ]
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11460
    
  94

Moved to the HTML and JavaScript forum.


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11460
    
  94

The first thing that sticks out is the 'while' command is almost certainly incorrect. Reformatting your code, I can see that the while command is actually just a single statement:So, unless you happen to enter 'z' as the very first letter, you will end up in an infinite loop whereby you are just setting the value for 'total' over and over again.

To fix that particular problem, change the code to:Try that - you should then see that the program works (for a given value of 'works' ). A hint for the next problem - what is the difference between '=' and '=='?

Regards, Andrew
Ricci Dolan
Greenhorn

Joined: Jul 09, 2006
Posts: 5
thanks for that,

== equal to
= equals
?

either way it still wont count the number of a's

[ July 09, 2006: Message edited by: Ricci Dolan ]
[ July 09, 2006: Message edited by: Ricci Dolan ]
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11460
    
  94

So what results are you getting now?

Regards, Andrew
Ricci Dolan
Greenhorn

Joined: Jul 09, 2006
Posts: 5
well....



it counts the number of times i enter a string containing "a" not the number of "a" in a string

and


doesnt count at all
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11460
    
  94

OK, this is a good start:
Originally posted by Ricci Dolan:
== equal to
= equals
But you didnt think of all the places where you might be doing an assignment rather than a comparison. Specifically:Your statement is "if (assignment)" and in many languages (Javascript included) this will always evaluate to true.

Does that help?

Regards, Andrew
[ July 09, 2006: Message edited by: Andrew Monkhouse ]
Nimchi Yung
Ranch Hand

Joined: Jan 27, 2004
Posts: 71
if you need to count how many occurences of 'a' inside a string, you
could use string.charAt(index) to compare.

something like:



--Nimchi
Ricci Dolan
Greenhorn

Joined: Jul 09, 2006
Posts: 5
Your statement is "if (assignment)" and in many languages (Javascript included) this will always evaluate to true.

Does that help?

Regards, Andrew

[ July 09, 2006: Message edited by: Andrew Monkhouse ][/qb]<hr></blockquote>

thanks for sticking with me Andrew you are helping
ok I get what your saying and have changed the code



as i read the code it makes sence in my head and i think it should work
if not equal to z it keeps asking for the string
if equal to A it adds 1 to the total
BUT
it doesnt.

Also thanks Nimchi your program works fine but i think its using commands that are above my level and it doesnt really help my understanding why my exising code doesnt work.Thanks again though.
[ July 10, 2006: Message edited by: Ricci Dolan ]
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11460
    
  94

Ok, we are getting closer all the time

You have the following line of code:Unfortunately this line does not give tell the Javascript interpreter what it should do with the result of adding "total + 1". That is, "total + 1" will give us a value, but that value is not assigned to anything.

Your previous version of the code had which is assigning the value created by the "total + 1" back into "total".

So try putting that section only of the code back to the way it was. Keep your new if (letter=='a') statement, just change what happens when that conditional is true.

Regards, Andrew

P.S. Do you realize that this code can only handle a single character being entered at a time? That is, if you enter "astrology" it will not match "a", likewise entering "zoo" wont match "z" (so you wont exit).
Ricci Dolan
Greenhorn

Joined: Jul 09, 2006
Posts: 5
yup andrew i realised this last night
i knew that it stopped when i entered a single Z so i entered a single "a" and found that it was counting

thanks for everyones help ive finaly moved on from this code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why doesnt my code work