• Post Reply Bookmark Topic Watch Topic
  • New Topic

counting instance of numbers in a particular string  RSS feed

 
Steven Hofmann
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!