| Author |
counting instance of numbers in a particular string
|
Steven Hofmann
Greenhorn
Joined: Oct 24, 2010
Posts: 17
|
|
OK, one last question for the night. I took in a string from the user named 'string'. I wrote a FOR loop to go through each individual character in the string, and what I tried to do was to have a variable counter "hasnum" to ++ every time it comes across a number in the string.
Basically, if a string input is Q1W2E3, I am trying to make "hasnum" be "3" since there are three numbers in the string.
Here is my code snippet that doesn't seem to be working right:
length was set to equal string.length(). I guess my approach was to say if the character was between and including 0 and 9, it would bump "hasnum" by one. Is this sound, or no? Right now, "hasnum" is still equaling zero.
Thanks!
Steve.
EDIT: I also used the following to get the user's input:
I have to use a string (assignment criteria) and also count its length (which I was able to do).
|
 |
Kurt Van Etten
Ranch Hand
Joined: Sep 07, 2010
Posts: 98
|
|
Hi Steven,
Your problem is that you are comparing characters in the input string to the numbers 0 and 9, while you probably intended to compare the characters to the digits '0' and '9' (which are chars).
|
 |
Steven Hofmann
Greenhorn
Joined: Oct 24, 2010
Posts: 17
|
|
Kurt Van Etten wrote:Hi Steven,
Your problem is that you are comparing characters in the input string to the numbers 0 and 9, while you probably intended to compare the characters to the digits '0' and '9' (which are chars).
Thanks for the reply.
Would it be better to load the char to a temporary variable and say if == 0 || ==1 || ==2 and so forth?
EDIT: tried it, doesn't work lol :-\
|
 |
Steven Hofmann
Greenhorn
Joined: Oct 24, 2010
Posts: 17
|
|
I changed it around to this and it works now, thanks for pointing me in the right direction.
[edit]Please avoid long lines in code tags; they make it difficult to read the whole text.[/edit]
|
 |
Kurt Van Etten
Ranch Hand
Joined: Sep 07, 2010
Posts: 98
|
|
Actually, you probably just want to do the easy thing here, and put single quotes around the 0 and the 9 in your two original comparisons. You could also do something similar if you wanted to count, say, the number of upper case letters, by comparing against 'A' and 'Z'. (By the way, there are built-in methods for detecting things like digits, and upper or lower case letters, that you will undoubtedly be learning about in the near future. If you're curious, take a look at the Character class in the Java API: http://download.oracle.com/javase/6/docs/api/java/lang/Character.html)
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
Kurt is right. chars are numbers with special formatting properties. That means you can use operators like > and < on them. Since '0' through '9' is one continuous block (as are 'a' through 'z' and 'A' through 'Z'; not so for combining upper case and lower case though!) you can simply use this:
Note the quotes Kurt mentioned and you forgot.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
 |
|
|
subject: counting instance of numbers in a particular string
|
|
|